Master JavaScript logical operators with practical examples and best practices
JavaScript’s logical operators (||, &&, ??) are powerful tools that go way beyond simple true/false operations. Let’s dive into how they work, explore some clever use cases, and see how they can make your code cleaner and more elegant.
The AND Operator &&: Elegant Conditional Execution
The AND operator (&&) is your friend for conditional execution. It returns the first “falsy” value or the last value if everything is truthy. This creates a clean way to run code conditionally.
The && operator in JavaScript does something subtle - it returns the last value if all conditions are true, or the first falsy value it encounters. This behavior enables two powerful patterns:
This works because:
If isValid is false, && returns false and stops there
If isValid is true, && evaluates and returns the result of sendToServer(data)
The magic happens because:
React ignores false, null, and undefined in JSX
When isLoggedIn is true, && returns the component
When isLoggedIn is false, && returns false (which React ignores)
Watch Out For These Cases
Any falsy value (0, '', null, undefined, false) will short-circuit &&
In React, only false, null, and undefined are truly “invisible”
Other falsy values like 0 or '' will actually render
The Nullish Coalescing Operator ??: Smart Defaults
Think of ?? as a smarter way to set default values. Unlike || which triggers on any falsy value, ?? only triggers on null or undefined. Here’s why this difference matters:
This becomes super useful when working with numbers or strings where 0 or "" are meaningful values:
The ?? operator really shines when:
Working with numbers where 0 is valid
Handling strings where empty string is meaningful
Processing API responses where you need to distinguish between “not set” (null/undefined) and “intentionally empty” (0/"")
You can also chain it for multiple fallbacks:
Choosing the Right Operator
Let’s look at common scenarios and which operator fits best:
Need a default value? Here’s how to choose:
Need conditional execution? Use &&
Need multiple fallbacks? Combine them with parentheses:
These operators are more than just syntax shortcuts - they’re tools for writing cleaner, more expressive code. Next time you’re about to write an if statement, consider if one of these operators might make your code more elegant.
My suggestion is to start with the nullish coalescing operator ?? for default values. It’s the newest addition to JavaScript, and it often leads to fewer surprises than the older OR operator ||
Related Articles
If you enjoyed this article, you might find these related pieces
interesting as well.
Recommended Engineering Resources
Here are engineering resources I've personally vetted and use. They
focus on skills you'll actually need to build and scale real projects -
the kind of experience that gets you hired or promoted.
Imagine where you would be in two years if you actually took the time to
learn every day. A little effort consistently adds up, shaping your
skills, opening doors, and building the career you envision. Start now,
and future you will thank you.