JavaScript Operators
JavaScript Operators Interview with follow-up questions
1. What are the different types of operators available in JavaScript?
JavaScript groups operators into several categories:
- Arithmetic:
+,-,*,/,%(remainder), and**(exponentiation), plus++/--. - Assignment:
=and its compound forms+=,-=,*=,/=,**=, and the logical assignments&&=,||=,??=. - Comparison:
===/!==(strict — preferred),==/!=(loose, with coercion — avoid), and>,<,>=,<=. - Logical:
&&,||,!, plus nullish coalescing??(defaults only onnull/undefined). - Bitwise:
&,|,^,~,<<,>>,>>>. - Ternary / conditional:
condition ? a : b. - Unary type/relational:
typeof,instanceof,in,delete,void, plus spread/rest.... - Optional chaining
?.for safe property/method access.
The follow-up interviewers want: know that ?? differs from || (falsy vs nullish), that === avoids the coercion traps of ==, and that &&/||/?? short-circuit — they stop evaluating once the result is known, which is also why they return one of the operands rather than a strict boolean.
Follow-up 1
Can you explain the difference between '==' and '===' operators?
Yes, the '' operator is used for loose equality comparison, while the '=' operator is used for strict equality comparison.
- The '==' operator compares the values of two operands after performing type coercion if necessary. For example, '1' == 1 would return true because the string '1' is coerced to a number before comparison.
- The '===' operator compares the values and types of two operands without performing any type coercion. For example, '1' === 1 would return false because the string '1' is not strictly equal to the number 1.
Follow-up 2
What is the use of the 'typeof' operator?
The 'typeof' operator is used to determine the type of a variable or expression in JavaScript. It returns a string indicating the type of the operand.
For example:
typeof 42; // returns 'number'
typeof 'Hello'; // returns 'string'
typeof true; // returns 'boolean'
typeof undefined; // returns 'undefined'
typeof null; // returns 'object' (this is a known bug in JavaScript)
typeof [1, 2, 3]; // returns 'object'
typeof {name: 'John', age: 30}; // returns 'object'
Note that the 'typeof' operator returns 'object' for arrays and objects, which is a known quirk in JavaScript.
Follow-up 3
How does the ternary operator work in JavaScript?
The ternary operator in JavaScript is a conditional operator that takes three operands and returns a value based on a condition.
The syntax of the ternary operator is as follows:
condition ? expression1 : expression2;
- If the condition is true, the expression1 is evaluated and returned.
- If the condition is false, the expression2 is evaluated and returned.
For example:
var age = 18;
var status = (age >= 18) ? 'adult' : 'minor';
console.log(status); // Output: 'adult'
In this example, if the age is greater than or equal to 18, the status variable is assigned the value 'adult'. Otherwise, it is assigned the value 'minor'.
Follow-up 4
What is the difference between '&&' and '||' operators?
The '&&' (logical AND) and '||' (logical OR) operators are used to combine or negate boolean values in JavaScript.
- The '&&' operator returns true if both operands are true, and false otherwise. For example,
true && truereturns true, whiletrue && falsereturns false. - The '||' operator returns true if at least one of the operands is true, and false otherwise. For example,
true || falsereturns true, whilefalse || falsereturns false.
These operators can also be used with non-boolean values. In such cases, they perform type coercion before evaluating the operands. For example, 0 && 'Hello' returns 0, while '' || 'World' returns 'World'.
2. How does the '==' operator work in JavaScript?
== is the loose (abstract) equality operator. It compares two values after coercing them to a common type, which is exactly why it's discouraged in modern code — prefer ===.
The coercion rules produce results that surprise people:
0 == ""; // true ("" → 0)
0 == "0"; // true
"" == "0"; // false (both strings, no coercion → not equal)
0 == false; // true (false → 0)
null == undefined; // true (special case)
null == 0; // false (null only loosely equals undefined)
NaN == NaN; // false (NaN equals nothing)
[] == ![]; // true (the classic brain-teaser)
The key follow-ups:
null == undefinedistrue, butnull === undefinedisfalse.==between an object and a primitive calls the object'svalueOf/toStringfirst, which is how[] == 0becomestrue.- The reliable rule: use
===so you compare type and value. Reach for==only in one idiomatic case —x == nullto check for "null or undefined" in one shot. ForNaN, useNumber.isNaN; for-0/+0edge cases, useObject.is.
Follow-up 1
What is type coercion in JavaScript?
Type coercion in JavaScript is the automatic conversion of one data type to another. It occurs when an operator or function is applied to operands of different types. JavaScript has both implicit and explicit type coercion. Implicit type coercion occurs when JavaScript automatically converts a value from one type to another, while explicit type coercion occurs when the developer explicitly converts a value from one type to another using functions like 'Number()', 'String()', 'Boolean()', etc.
Follow-up 2
Can you give an example where '==' gives an unexpected result?
Yes, here's an example:
console.log(0 == false); // true
console.log('' == false); // true
console.log([] == false); // true
console.log([] == ''); // true
console.log(1 == true); // true
console.log(2 == true); // false
console.log('2' == 2); // true
console.log('2' == true); // false
Follow-up 3
Why is it recommended to use '===' instead of '=='?
It is recommended to use the '=' operator instead of '' in JavaScript because the '=' operator performs a strict equality comparison without any type coercion. It checks both the value and the type of the operands. This helps to avoid unexpected results that can occur due to type coercion. By using '=', you can ensure that the values being compared are of the same type and have the same value. This leads to more predictable and reliable code.
3. What is the significance of the '===' operator in JavaScript?
=== is the strict equality operator. It compares value and type with no coercion — if the operands are different types, it's immediately false. This predictability makes it the default in modern JavaScript.
5 === 5; // true
5 === "5"; // false (number vs string, no coercion)
true === 1; // false
null === undefined; // false
Follow-ups interviewers expect:
- For primitives it compares values; for objects/arrays/functions it compares references, so
{} === {}isfalse(two distinct objects), whileconst a = {}; a === aistrue. - Two edge cases
===gets "wrong" by intuition:NaN === NaNisfalse, and-0 === +0istrue.Object.isfixes both —Object.is(NaN, NaN)istrue,Object.is(-0, +0)isfalse. - Rule of thumb: always use
===/!==unless you specifically wantx == nullto catch bothnullandundefined.
Follow-up 1
How is '===' different from '=='?
The '=' operator checks for both value and type equality, while the '' operator only checks for value equality. This means that '=' will return true only if the values being compared are of the same type, whereas '' will perform type coercion and return true if the values can be considered equal after coercion.
Follow-up 2
Can you give an example where '===' gives a different result than '=='?
Sure! Here's an example:
const num1 = 5;
const num2 = '5';
console.log(num1 === num2); // false
console.log(num1 == num2); // true
In this example, '=' returns false because the values are of different types (number and string), while '' returns true because it performs type coercion and considers the values equal.
Follow-up 3
Why is '===' considered more reliable than '=='?
The '=' operator is considered more reliable than '' because it avoids unexpected behavior caused by type coercion. With '=', you can be sure that the values being compared are of the same type, which can help prevent bugs and make the code more predictable. It is generally recommended to use '=' for equality comparisons in JavaScript.
4. Can you explain the unary, binary, and ternary operators in JavaScript?
Operators are classified by how many operands they take:
Unary — one operand:
-x; // negation
!done; // logical NOT
typeof x; // type as a string
++count; // increment
Binary — two operands (the vast majority):
a + b; // arithmetic
a === b; // strict comparison
a && b; // logical
a ?? b; // nullish coalescing
Ternary — the only operator taking three operands, the conditional operator ?::
const access = age >= 18 ? "granted" : "denied";
Follow-ups worth knowing:
+is overloaded: binary+adds numbers or concatenates strings, while unary+coerces to number (+"3"→3).- The ternary is an expression (it returns a value), so it can sit inside template literals or JSX, unlike an
ifstatement. Avoid nesting ternaries deeply — readability suffers. ++/--differ in prefix vs postfix form:x++returns the old value,++xthe new one.
Follow-up 1
Can you give examples of each?
Sure! Here are some examples:
Unary operator example:
let x = 5;
let y = -x; // y will be -5
Binary operator example:
let a = 10;
let b = 5;
let sum = a + b; // sum will be 15
Ternary operator example:
let age = 18;
let message = (age >= 18) ? 'You are an adult' : 'You are a minor';
console.log(message); // 'You are an adult'
Follow-up 2
What is the precedence of these operators?
The precedence of operators determines the order in which they are evaluated. In JavaScript, unary operators have the highest precedence, followed by binary operators, and then ternary operators. Within each category, operators are evaluated from left to right.
For example, in the expression a + b * c, the multiplication operator (*) has higher precedence than the addition operator (+), so b * c will be evaluated first, and then the result will be added to a.
It's important to note that parentheses can be used to change the precedence of operators. Expressions within parentheses are evaluated first, regardless of the precedence of the operators involved. For example, in the expression (a + b) * c, the addition operator (+) is evaluated first because it is within parentheses, and then the result is multiplied by c.
Follow-up 3
How can parentheses be used to change the precedence?
Parentheses can be used to explicitly specify the order of evaluation in an expression, overriding the default precedence of operators. Expressions within parentheses are always evaluated first, regardless of the precedence of the operators involved.
For example, in the expression a + b * c, the multiplication operator (*) has higher precedence than the addition operator (+), so b * c will be evaluated first, and then the result will be added to a. However, if we want the addition to be evaluated first, we can use parentheses like this: (a + b) * c.
Similarly, if we want to change the precedence of ternary operators, we can use parentheses to group the expressions. For example, (condition1 ? expression1 : expression2) ? expression3 : expression4.
5. How do assignment operators work in JavaScript?
Assignment operators store a value in a variable. The base operator is =, which assigns the right-hand value to the left-hand binding:
let x = 5;
Compound assignments combine an operation with assignment — x += 1 is shorthand for x = x + 1. The full set: +=, -=, *=, /=, %=, **=, plus the bitwise ones (&=, |=, ^=, <<=, >>=).
Newer and very interview-relevant are the logical assignment operators (ES2021), which assign conditionally and short-circuit:
let a;
a ??= 10; // assigns 10 only if a is null/undefined → 10
config.cache ||= {}; // assigns only if left is falsy
user &&= user.trim(); // assigns only if left is truthy
Follow-ups:
??=is the safe way to set a default — unlike||=, it won't overwrite a valid0or"".- Assignment is itself an expression returning the assigned value, which enables
a = b = 0(both become0). - Don't confuse
=(assign) with==/===(compare) — a frequent bug source insideifconditions.
Follow-up 1
What is the difference between '=', '+=', and '-=' operators?
The '=' operator is the basic assignment operator, as mentioned earlier. The '+=' operator is a shorthand assignment operator that adds the value on the right-hand side to the variable on the left-hand side. For example:
let x = 5;
x += 3; // x is now 8
```The '-=' operator is similar to the '+=' operator, but it subtracts the value on the right-hand side from the variable on the left-hand side. For example:
```javascript
let x = 5;
x -= 3; // x is now 2
Follow-up 2
Can you give an example of each?
Sure! Here are examples of each assignment operator:
let x = 5; // '=' operator
let y = 10;
y += 3; // '+=' operator
let z = 8;
z -= 4; // '-=' operator
Follow-up 3
What is the use of the '++' and '--' operators?
The '++' operator is the increment operator, and it is used to increase the value of a variable by 1. The '--' operator is the decrement operator, and it is used to decrease the value of a variable by 1. These operators can be used both as prefix and postfix. For example:
let x = 5;
x++; // x is now 6
let y = 10;
y--; // y is now 9
Live mock interview
Mock interview: JavaScript Operators
- Read your scene and goals
- Talk it out; goals tick off live
- Get a score and stronger lines
Your voice and your AI key never touch our servers; the key stays in this browser and is sent only to Google. Only your round scores are saved to track progress.