If you are still writing if (!settings.theme) settings.theme = 'dark'; or even settings.theme = settings.theme || 'dark';, it is time to simplify. Modern JavaScript gives us logical assignment operators that combine logical operations with assignment in a clean, readable way.
By using settings.theme ||= 'dark', you only assign 'dark' if settings.theme is falsy. Even better, use the nullish coalescing assignment operator (??=) to only assign a value if the variable is strictly null or undefined. This prevents pesky bugs where valid falsy values, like 0 or an empty string, accidentally get overwritten.
Top comments (0)