Operators in JavaScript: ??, &&, || & Precedence

Easy100% Free~12 mins#operators#nullish-coalescing#optional-chaining#logical-operators#precedence
Interview Answer (30-Second Summary)

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');
4
5const user = { profile: null };
6console.log(user.profile?.name, user.settings?.theme ?? 'light');
7
8let x = 5;
9console.log(x++ + ++x, x);
10console.log(2 ** 3 ** 2, typeof null);
Predict Console Output
Interactive Challenge

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.

Call Stack (Top = Active)
Global
Lexical Scope / BindingsNo active local bindings
Console Stream
> 12 2 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 ++/--.

  1. + is special: if either side is a string, + concatenates. The other arithmetic operators always convert to numbers, so '3' - 1 is 2.
  2. Logical operators return values: a || b returns a if it is truthy, otherwise b; a && b returns a if it is falsy, otherwise b. a ?? b returns b only when a is null or undefined, which makes it the right default for values like 0 or ''. Optional chaining ?. returns undefined instead of throwing when the left side is null or undefined.
  3. Order matters: prefix ++x changes the value before it is read, postfix x++ after. ** groups from the right, and you cannot mix ?? with || or && without parentheses. typeof null is '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.

Q1.
What are the types of operators in JavaScript?

Arithmetic (+ - * / % **), assignment (= += -= ??= etc.), comparison (== === != !== < > <= >=), logical (&& || ! ??), bitwise (& | ^ ~ << >>), and others: typeof, instanceof, in, delete, the ternary ? :, optional chaining ?. and the comma operator.

|| 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.

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.

When either operand of + is a string, + concatenates. -, *, / and % only work on numbers, so they convert their operands first.

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.

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.

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