JavaScript Operators Interview Questions and Answers
Review JavaScript operators, precedence, equality, and the effects of implicit and explicit conversions.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
Theoretical JavaScript Interview Questions
Type coercion
is JavaScript quietly converting values behind your back to make an operation work. When you write "5" + 3, JS turns 3 into a string and gives you "53". That's implicit coercion - the engine decides on its own. Explicit coercion is when you do it yourself with Number("5") or String(42). The key interview insight: implicit coercion is the root cause of most == vs === confusion, and why experienced devs almost always prefer strict equality.
Operators are the verbs of your code. They do things to values: add them, compare them, assign them, check conditions. Without operators, your variables would just sit there doing nothing. They range from the familiar math symbols (+, -, *) to logical checks (&&, ||) and even structural ones like typeof and instanceof. Understanding operators is the foundation for writing any expression in JavaScript.
An operand is the value that an operator works on. In a + b, both a and b are operands and + is the operator. Think of it like a sentence: the operator is the verb, and operands are the nouns it acts on.
Some operators take one operand (unary, like !true), most take two (binary, like 5 + 3), and the ternary operator takes three. Knowing this terminology helps when reading documentation or error messages.
let a = 5; // Operand (variable)
let b = 3; // Operand (variable)
let result = a + b; // Operator (+) acts on 'a' and 'b' to produce a result
console.log(result); // Output will be 8 (sum of 'a' and 'b')
Comparison operators ask questions about values and always answer with true or false. You've got greater than (>), less than (<), greater-or-equal (>=), less-or-equal (<=), loose equality (==), strict equality (===), and their "not" versions. These are the backbone of every if statement, while loop, and for condition you'll ever write. The biggest interview topic here is == vs === - stick with === unless you have a specific reason not to.
== does a loose comparison that tries to convert types first, which leads to weird results like "0" == false being true. === is strict - it checks both value and type with no conversion, so "5" === 5 is false. In real projects, always default to ===. The only common exception is checking null == undefined (which is true with ==) as a shortcut for checking both at once.
console.log(5 == "5"); // true
console.log(5 === "5"); // false
console.log(null == undefined); // true
console.log(null === undefined); // false
Assignment operators put values into variables. The basic = just assigns, but the compound ones like +=, -=, *=, /= do math and assign in one step. So x += 5 is shorthand for x = x + 5. They exist for every arithmetic operator plus a few others like &&= and ||= (added in ES2021). Using them makes your code shorter, but don't sacrifice readability for
assignment
cleverness - x = x + 5 is perfectly fine if it's clearer in context.
Logical operators combine or flip boolean values. AND (&&) returns true only when both sides are true - great for "this AND that must be valid." OR (||) returns true if either side is true - perfect for fallbacks. NOT (!) flips true to false and vice versa. Here's the real-world trick:
logical
operators in JavaScript don't always return true/false. They return the actual value that determined the result. That's why name || "Anonymous" works as a default value pattern - if name is falsy, you get "Anonymous" back.
The
typeof
operator returns a string telling you what type a value is - "number", "string", "boolean", "object", "function", "undefined", or "symbol". It's your go-to for type checking before performing operations. The famous gotcha: typeof null returns "object", which is a bug from JavaScript's earliest days that was never fixed for backward compatibility. Also, typeof on an undeclared variable returns "undefined" instead of throwing an error, which can be useful for feature detection.
Operator precedence is the pecking order that determines which operations run first when multiple operators appear in one expression. Just like in math where multiplication happens before addition, JavaScript has its own hierarchy: grouping () > member access . > NOT ! > arithmetic > comparison > logical AND > logical OR > assignment. When in doubt, use parentheses to make your intent explicit. Code that relies on obscure precedence rules is hard to read and a breeding ground for bugs.
++ adds 1 and -- subtracts 1. The subtle part is prefix vs postfix: ++x increments first and returns the new value, while x++ returns the current value and then increments. This distinction only matters when the result is used in an expression - in a standalone statement like a for-loop counter, both behave the same.
The % operator gives you what's left over after division - 9 % 4 is 1. It's useful for checking if a number is even (n % 2 === 0), clamping array indices to wrap around, or any time you need cyclic behavior. Watch out with negative numbers - in JavaScript the sign of the result follows the dividend, so -7 % 3 is -1, not 2.
instanceof walks up an object's prototype chain to check if a constructor's prototype appears anywhere in it - in other words, it tells you whether an object was created from a particular class or constructor. One gotcha: it doesn't work on primitives ("hello" instanceof String is false), and it can give unexpected results across iframes since they have separate global contexts. For primitive type checks, typeof is your friend.
console.log([] instanceof Array); // true
console.log(new Date() instanceof Date); // true
console.log("hello" instanceof String); // false
The bug with || is that it treats 0, '', and false as falsy, so count || 0 always returns 0 even when count is a valid 0. The ?? operator only triggers the fallback for null or undefined - so count ?? 0 correctly returns 0 when count is 0. Anytime you have a value that could legitimately be 0, false, or an empty string, use ?? instead of ||.
Use `??` when a fallback should apply only to `null` or `undefined`; `||` also falls back for values such as `0`, `false`, and `""`. This preserves valid falsy values.
Optional chaining stops evaluating the rest of that chain when the value immediately before `?.` is `null` or `undefined`, yielding `undefined`. It does not suppress errors from unrelated expressions or from a non-nullish value that lacks a callable method.
`==` applies specified coercion rules before comparison, which can produce non-obvious results across strings, numbers, booleans, and objects. `===` avoids those coercions and is generally clearer unless coercive equality is intentional.
JavaScript syntax forbids mixing `??` directly with `&&` or `||` without parentheses, avoiding ambiguous-looking fallback logic. Add parentheses to explicitly state which operation should be evaluated first.
`&&` and `||` return one of their operand values rather than necessarily returning a Boolean; they short-circuit based on truthiness. This enables guarded expressions but can propagate non-Boolean values.
Use parentheses to make intended grouping explicit, especially when combining arithmetic, comparisons, and logical operators. Precedence controls parsing, but explicit grouping improves reviewability and reduces mistaken assumptions.
The conditional operator is right-associative, so nested conditionals group from the right. Parentheses or ordinary `if` statements are often clearer than deeply nested `?:` expressions.