π¨ JavaScript Β· Topic 3 of 20
Operators β JavaScript Questions with Answers
30 practice questions on Operators, each with the correct answer and a clear explanation of why it is right. Read through them as revision notes, or take this topic as an interactive quiz to test yourself.
7 easy Β· 14 medium Β· 9 hard Β· Part of JavaScript: The language of the web - basics to advanced, 20 topics.
Easy questions (7)
Q1. easy
What is the output?
console.log(7 / 2);
- A 3.5 - JavaScript division always gives a decimal when needed
- B 3
- C 4
- D 3.0
Answer: A β 3.5 - JavaScript division always gives a decimal when needed
There is only one number type, so 7 / 2 is 3.5. To get a whole number use Math.floor(7 / 2) or Math.trunc(7 / 2).
DivisionMath.floor
Q2. easy
What does the % (modulo) operator return?
console.log(7 % 2);
console.log(10 % 3); // 1
console.log(8 % 2); // 0 - even
- A 1 - the REMAINDER after division
- B 3.5
- C 3
- D 0.5
Answer: A β 1 - the REMAINDER after division
% gives the remainder: 7 divided by 2 is 3 remainder 1. The standard even-number test is n % 2 === 0.
ModuloEven/odd
Q3. easy
What does ** do?
console.log(2 ** 3);
console.log(2 ** 3); // 8
console.log(9 ** 0.5); // 3 - square root
- A 8 - exponentiation (2 to the power 3)
- B 6
- C 23
- D 5
Answer: A β 8 - exponentiation (2 to the power 3)
** is the exponent operator (ES2016): 2 ** 3 = 8. The older equivalent is Math.pow(2, 3).
ExponentMath.pow
Q6. easy
What does += do?
let x = 10;
x += 5;
let s = "Hello";
s += " World"; // "Hello World"
- A x becomes 15 (x = x + 5)
- B x becomes 5
- C x becomes 105
- D SyntaxError
Answer: A β x becomes 15 (x = x + 5)
Compound assignment: += adds and reassigns. Others: -=, *=, /=, %=, **=. With strings, += concatenates.
Compound assignment
Q11. easy
Which operator checks equality WITHOUT type conversion?
console.log(5 === "5"); // false - different types
console.log(5 == "5"); // true - coerced
- A ===
- B ==
- C =
- D !=
Answer: A β ===
=== (strict equality) compares value AND type - no coercion. == converts types first, which causes surprises. Always prefer ===.
Strict equalityCoercion
Q12. easy
What is the difference between = and ==?
- A = ASSIGNS a value; == COMPARES two values
- B They are the same
- C == assigns; = compares
- D = only works with numbers
Answer: A β = ASSIGNS a value; == COMPARES two values
One equals sign stores a value (x = 5). Two compare (x == 5). Three compare strictly (x === 5). Using = inside an if is a classic bug.
AssignmentComparison
Q14. easy
What does && (logical AND) return?
console.log(true && false);
console.log(true && true); // true
console.log(true && false); // false
console.log(false || true); // true
- A false - both sides must be truthy
- B true
- C undefined
- D 0
Answer: A β false - both sides must be truthy
&& is true only when BOTH operands are truthy. || (OR) is true when at least one is.
Logical operators
Medium questions (14)
Q4. medium
What is the output?
let x = 5;
console.log(x++);
console.log(x);
let a = 5;
console.log(a++); // 5 (then a is 6)
let b = 5;
console.log(++b); // 6
- A 5 then 6 - POST-increment returns the old value first, then increments
- B 6 then 6
- C 5 then 5
- D 6 then 7
Answer: A β 5 then 6 - POST-increment returns the old value first, then increments
x++ (postfix) yields the current value 5, THEN adds 1. ++x (prefix) would add first and yield 6.
IncrementPrefix vs postfix
Q5. medium
What is the output?
let x = 5;
console.log(++x);
- A 6 - PRE-increment adds first, then returns
- B 5
- C 7
- D undefined
Answer: A β 6 - PRE-increment adds first, then returns
++x increments to 6 and returns the new value. The difference only matters when you use the result in the same expression.
Increment
Q9. medium
What is the output?
console.log(2 + 3 + "4");
console.log(2 + 3 + "4"); // "54"
console.log("4" + 2 + 3); // "423" - order matters!
- A "54" - 2 + 3 is calculated first (5), then "4" turns it into a string
- B "234"
- C 9
- D "9"
Answer: A β "54" - 2 + 3 is calculated first (5), then "4" turns it into a string
+ evaluates left to right: 2 + 3 = 5 (both numbers), then 5 + "4" concatenates to "54".
AssociativityCoercion
Q10. medium
What is the output?
console.log(2 + 3 * 4);
- A 14 - multiplication happens before addition
- B 20
- C 24
- D 9
Answer: A β 14 - multiplication happens before addition
Standard precedence: * before +. Use parentheses to change it: (2 + 3) * 4 is 20.
Operator precedence
Q13. medium
Which operator means 'strictly NOT equal'?
console.log(5 !== "5"); // true - different types
console.log(5 != "5"); // false - coerced to equal
- A !==
- B !=
- C <>
- D not ===
Answer: A β !==
!== is the strict inequality operator (no type conversion), the counterpart of ===. != is the loose version.
Inequality
Q18. medium
What does the ternary operator do?
let status = age >= 18 ? "adult" : "minor";
const msg = score > 50 ? "Pass" : "Fail";
- A Returns "adult" if the condition is true, otherwise "minor" - a one-line if/else
- B Compares three values
- C Loops three times
- D SyntaxError
Answer: A β Returns "adult" if the condition is true, otherwise "minor" - a one-line if/else
condition ? valueIfTrue : valueIfFalse is JavaScript's only three-operand operator. Great for short assignments; avoid deep nesting.
TernaryConditional expression
Q19. medium
What does ! (logical NOT) do?
console.log(!true, !0);
console.log(!true); // false
console.log(!0); // true
console.log(!""); // true
- A true becomes false; 0 (falsy) becomes true
- B Both become true
- C Both become false
- D SyntaxError
Answer: A β true becomes false; 0 (falsy) becomes true
! converts its operand to a boolean and flips it. !0 is true because 0 is falsy.
Logical NOTTruthiness
Q21. medium
What is the output?
console.log(5 > 3 && 2 > 1);
- A true - both comparisons are true
- B false
- C 5
- D SyntaxError
Answer: A β true - both comparisons are true
Comparisons produce booleans, and && requires both to be true. Comparison operators have higher precedence than &&, so no parentheses are needed.
ComparisonLogical operators
Q23. medium
What does the optional chaining operator ?. do?
user?.address?.city
const city = user?.address?.city ?? "Unknown";
- A Safely reads nested properties - returns undefined instead of throwing if something is null/undefined
- B Makes properties optional to declare
- C Creates the property if missing
- D Is a syntax error
Answer: A β Safely reads nested properties - returns undefined instead of throwing if something is null/undefined
Without ?., reading user.address.city when address is undefined throws a TypeError. Optional chaining short-circuits to undefined instead - very handy with API data.
Optional chainingNullish coalescing
Q24. medium
What does the spread operator ... do here?
const combined = [...arr1, ...arr2];
const a = [1, 2], b = [3];
console.log([...a, ...b]); // [1, 2, 3]
console.log({ ...obj1, ...obj2 }); // merged object
- A Expands both arrays' elements into a new array (concatenation)
- B Multiplies the arrays
- C Nests them
- D Deletes duplicates
Answer: A β Expands both arrays' elements into a new array (concatenation)
Spread unpacks iterables into a new array or object. It is the modern way to copy or merge without mutating the originals.
SpreadRest parameters
Q25. medium
What is the output?
console.log(10 + +"5");
console.log(+"5"); // 5 (number)
console.log(+true); // 1
console.log(+"abc"); // NaN
- A 15 - the unary plus converts "5" to the number 5
- B "105"
- C NaN
- D SyntaxError
Answer: A β 15 - the unary plus converts "5" to the number 5
A single + before a value is the unary plus, a compact way to convert to a number. So 10 + 5 = 15.
Unary plusType conversion
Q27. medium
What does the comma operator do?
let x = (1, 2, 3);
- A Evaluates each expression and returns the LAST one - x is 3
- B Creates an array
- C SyntaxError
- D x becomes 1
Answer: A β Evaluates each expression and returns the LAST one - x is 3
The comma operator evaluates left to right and yields the final value. Rarely used deliberately, but it appears in compact for-loop headers.
Comma operator
Q29. medium
What does the delete operator do?
delete user.age;
- A Removes the property from the object
- B Deletes the whole object
- C Sets the property to null
- D Deletes a variable
Answer: A β Removes the property from the object
delete removes a property entirely (afterwards 'age' in user is false). It does not work on variables, and on arrays it leaves a hole rather than shifting elements - use splice() for arrays.
deleteObjects
Q30. medium
What does the in operator check?
console.log("name" in user);
console.log("name" in { name: "Ana" }); // true
console.log(1 in [10, 20]); // true - index 1!
console.log([10, 20].includes(20)); // true - value
- A Whether the object has a property with that KEY
- B Whether a value exists in the object
- C Whether a string contains 'name'
- D Whether user is inside name
Answer: A β Whether the object has a property with that KEY
in tests for a property key (including inherited ones). For arrays it checks INDEXES, not values - use includes() for array values.
in operatorincludes
Hard questions (9)
Q7. hard
What is the output?
console.log("5" + 3);
console.log("5" + 3); // "53"
console.log(5 + 3); // 8
- A "53" - the number is converted to a string and concatenated
- B 8
- C NaN
- D TypeError
Answer: A β "53" - the number is converted to a string and concatenated
When either operand of + is a string, JavaScript converts the other to a string and joins them. This is the most common coercion surprise.
CoercionConcatenation
Q8. hard
What is the output?
console.log("5" - 3);
console.log("5" - 3); // 2
console.log("5" * 2); // 10
console.log("5" + 3); // "53" - the odd one out
- A 2 - unlike +, the minus operator converts strings to NUMBERS
- B "53"
- C "2"
- D NaN
Answer: A β 2 - unlike +, the minus operator converts strings to NUMBERS
Only + has the string-concatenation meaning. -, *, / and % always try numeric conversion, so "5" - 3 is 2. This asymmetry catches many beginners.
CoercionOperators
Q15. hard
What is the output?
console.log(0 || "hello");
const name = userInput || "Guest"; // fallback if empty
- A "hello" - || returns the first TRUTHY value, not just true/false
- B true
- C 0
- D false
Answer: A β "hello" - || returns the first TRUTHY value, not just true/false
Logical operators return one of the OPERANDS, not booleans. 0 is falsy so || moves on and returns "hello". A common way to supply a default value.
Short-circuitDefaults
Q16. hard
What is the output?
console.log("hi" && "bye");
- A "bye" - && returns the LAST value if all are truthy (or the first falsy one)
- B "hi"
- C true
- D false
Answer: A β "bye" - && returns the LAST value if all are truthy (or the first falsy one)
&& stops at the first falsy operand and returns it; if none are falsy it returns the last one. Used for conditional execution: isLoggedIn && showDashboard().
Short-circuitLogical AND
Q17. hard
What is the difference between || and ?? (nullish coalescing)?
const count = 0;
console.log(count || 10); // 10 - wrong!
console.log(count ?? 10); // 0 - correct
- A || falls back on ANY falsy value (0, "", false); ?? falls back only on null or undefined
- B They are identical
- C ?? is deprecated
- D ?? only works with numbers
Answer: A β || falls back on ANY falsy value (0, "", false); ?? falls back only on null or undefined
If a valid value can be 0 or "", || wrongly replaces it. ?? treats only null/undefined as 'missing', which is usually what you want for defaults.
Nullish coalescingDefaults
Q20. hard
What does the double NOT !! do?
console.log(!!"hello");
console.log(!!"hello"); // true
console.log(!!0); // false
console.log(Boolean(0)); // false - clearer alternative
- A Converts any value to its boolean equivalent - here true
- B Nothing - it cancels out
- C Returns the string
- D SyntaxError
Answer: A β Converts any value to its boolean equivalent - here true
!! is a shorthand for Boolean(value): the first ! makes it a flipped boolean, the second flips it back. Common for turning a value into a real true/false.
Boolean conversionTruthiness
Q22. hard
What is the output?
console.log(1 < 2 < 3, 3 > 2 > 1);
- A true false - chaining does NOT work as maths; the second becomes (3>2)=true, then true>1 is false
- B true true
- C false false
- D SyntaxError
Answer: A β true false - chaining does NOT work as maths; the second becomes (3>2)=true, then true>1 is false
Comparisons evaluate left to right into booleans. 3 > 2 gives true, then true > 1 converts true to 1, and 1 > 1 is false. Never chain comparisons - write (3 > 2) && (2 > 1).
- A favourite trick question.
Chained comparisonCoercion
Q26. hard
What is the output?
console.log(typeof (5 > 3));
const isAdult = age >= 18; // store the boolean
return n % 2 === 0; // return it
- A "boolean"
- B "number"
- C "string"
- D "object"
Answer: A β "boolean"
A comparison evaluates to true or false, whose type is "boolean". You can store or return comparisons directly instead of writing if/else.
BooleansComparison
Q28. hard
What are the bitwise operators & | ^ ~ << >> used for?
console.log(5 & 3); // 1 (0101 & 0011)
console.log(5 | 3); // 7
console.log(5 << 1); // 10 (shift left = x2)
- A Operating on the individual bits of numbers - flags, masks, low-level maths
- B Logical AND/OR only
- C String manipulation
- D Comparing objects
Answer: A β Operating on the individual bits of numbers - flags, masks, low-level maths
They convert numbers to 32-bit integers and work bit by bit. Everyday web code rarely needs them, but they appear in permissions flags, graphics and optimisation tricks.
BitwiseBinary