Is there a null coalescing operator in JavaScript?
Hello everyone! I'm a beginner JS developer, and I have a problem with handling values that can be null or undefined.\n\nLet's say I have a function that gets user settings, and if some property is missing, I want to substitute a default value. Currently I do it like this:\n\n```javascript\nfunction getSettings(user) {\n const theme = user.theme || 'light';\n const fontSize = user.fontSize || 16;\n return { theme, fontSize };\n}\n```\n\nBut this doesn't work quite correctly. For example, if the user explicitly specified `theme: ''` (empty string) or `fontSize: 0`, the `||` operator treats them as falsy and substitutes the default value. But I need the substitution to happen only if the value is actually null or undefined.\n\nI've heard that in some
No answers yet.
Log in to leave an answer
Blogs
Code Review in JavaScript: What to Check and How to Give Feedback
Algorithms and Data Structures in Go: A Practical Guide
How to Merge Two Lists in Java: 5 Working Methods with Examples
Turning std::string into int: A Battle of Performance and Convenience in C++