Operators in JavaScript: ??, &&, || & Precedence
Operators in JavaScript include arithmetic (+, -, *, /, %, **), assignment, comparison, logical (&&, ||, !, ??), optional chaining (?.) and typeof. The classic traps are that + concatenates when either side is a string, || falls back on any falsy value while ?? only on null or undefined, and typeof null is 'object'.
Key Learning Objectives
Predict + with strings and the other arithmetic operators with numeric strings.
Tell || from ?? and use && and || as value-returning operators.
Use optional chaining safely and know where it stops.
Read increment, exponent and typeof results, including the classic traps.
The Interview Problem
What do the six console.log calls print?
1console.log(1 + '2', '3' - 1, '3' * '4');2console.log(0 || 'default', 0 ?? 'default', null ?? 'default');3console.log(true && 'yes', null && 'never');45const user = { profile: null };6console.log(user.profile?.name, user.settings?.theme ?? 'light');78let x = 5;9console.log(x++ + ++x, x);10console.log(2 ** 3 ** 2, typeof null);
Predict Console Output
Select the option that matches what this code prints to the console:
12 2 12 / default 0 default / yes null / undefined light / 12 7 / 512 object
3 2 12 / default 0 default / yes null / undefined light / 12 7 / 512 object
12 2 12 / default default default / yes null / undefined light / 12 7 / 512 object
12 2 12 / default 0 default / yes null / undefined light / 11 7 / 64 object
V8 Engine Execution Trace
Step 1 of 6 (Line 1)1 + '2' concatenates to '12'. '3' - 1 and '3' * '4' convert the strings to numbers: 2 and 12.
Operators in JavaScript Explained
How the Operators in JavaScript Work
Operators in JavaScript fall into a few groups: arithmetic (+ - * / % **), assignment (= += ??=), comparison (== === < >), logical (&& || ! ??), and others such as typeof, ?., the ternary ? : and ++/--.
- + is special: if either side is a string,
+concatenates. The other arithmetic operators always convert to numbers, so'3' - 1is 2. - Logical operators return values:
a || breturnsaif it is truthy, otherwiseb;a && breturnsaif it is falsy, otherwiseb.a ?? breturnsbonly whenaisnullorundefined, which makes it the right default for values like 0 or''. Optional chaining?.returnsundefinedinstead of throwing when the left side isnullorundefined. - Order matters: prefix
++xchanges the value before it is read, postfixx++after.**groups from the right, and you cannot mix??with||or&&without parentheses.typeof nullis'object', so check for null with=== null.
Common Traps & Mistakes
Using || for defaults when 0, '' or false are valid values; use ?? instead.
Adding a number to a value read from an input field, which is a string, and getting concatenation.
Assigning through optional chaining (a?.b = 1), which is a SyntaxError.
FAANG Follow-Up Probes
Probe #1
What does [] + {} evaluate to, and why?
Probe #2
Why is 1 < 2 < 3 true but 3 > 2 > 1 false?
Probe #3
When would you use ??= or ||= in real code?
Operators in JavaScript Interview Questions
Key interview inquiries, edge cases, and architectural follow-ups for Operators.
What is the difference between || and ?? in JavaScript?
|| returns the right side when the left is any falsy value (0, '', false, NaN, null, undefined). ?? only does so for null or undefined, so 0 ?? 5 is 0 but 0 || 5 is 5.
What does the && operator return?
The first falsy operand, or the last operand if all are truthy. true && 'yes' is 'yes'; null && 'never' is null. That is why cond && <Component /> works in React.
Why does 1 + '2' give '12' but '3' - 1 give 2?
When either operand of + is a string, + concatenates. -, *, / and % only work on numbers, so they convert their operands first.
What is optional chaining?
a?.b returns undefined instead of throwing when a is null or undefined. It also works for calls (fn?.()) and indexes (arr?.[0]). It cannot be used on the left side of an assignment.
What is the difference between x++ and ++x?
x++ returns the old value and then increments; ++x increments first and returns the new value. With x = 5, x++ + ++x is 5 + 7 = 12.
Why is typeof null 'object'?
It is a bug from the first version of JavaScript that was kept for compatibility. Check for null with value === null.
More JavaScript interview questions for freshers
