πŸŽ“ Velnov

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

Key concepts in this topic

DivisionMath.floorModuloEven/oddExponentMath.powIncrementPrefix vs postfixCompound assignmentCoercionConcatenationOperatorsAssociativityOperator precedenceStrict equalityAssignmentComparisonInequalityLogical operatorsShort-circuitDefaultsLogical ANDNullish coalescingTernary

Jump to: Easy Β· Medium Β· Hard

Easy questions (7)

Q1. easy

What is the output? console.log(7 / 2);

  1. A 3.5 - JavaScript division always gives a decimal when needed
  2. B 3
  3. C 4
  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
  1. A 1 - the REMAINDER after division
  2. B 3.5
  3. C 3
  4. 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
  1. A 8 - exponentiation (2 to the power 3)
  2. B 6
  3. C 23
  4. 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"
  1. A x becomes 15 (x = x + 5)
  2. B x becomes 5
  3. C x becomes 105
  4. 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
  1. A ===
  2. B ==
  3. C =
  4. 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 ==?

  1. A = ASSIGNS a value; == COMPARES two values
  2. B They are the same
  3. C == assigns; = compares
  4. 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
  1. A false - both sides must be truthy
  2. B true
  3. C undefined
  4. 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
  1. A 5 then 6 - POST-increment returns the old value first, then increments
  2. B 6 then 6
  3. C 5 then 5
  4. 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);

  1. A 6 - PRE-increment adds first, then returns
  2. B 5
  3. C 7
  4. 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!
  1. A "54" - 2 + 3 is calculated first (5), then "4" turns it into a string
  2. B "234"
  3. C 9
  4. 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);

  1. A 14 - multiplication happens before addition
  2. B 20
  3. C 24
  4. 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
  1. A !==
  2. B !=
  3. C <>
  4. 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";
  1. A Returns "adult" if the condition is true, otherwise "minor" - a one-line if/else
  2. B Compares three values
  3. C Loops three times
  4. 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
  1. A true becomes false; 0 (falsy) becomes true
  2. B Both become true
  3. C Both become false
  4. 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);

  1. A true - both comparisons are true
  2. B false
  3. C 5
  4. 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";
  1. A Safely reads nested properties - returns undefined instead of throwing if something is null/undefined
  2. B Makes properties optional to declare
  3. C Creates the property if missing
  4. 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
  1. A Expands both arrays' elements into a new array (concatenation)
  2. B Multiplies the arrays
  3. C Nests them
  4. 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
  1. A 15 - the unary plus converts "5" to the number 5
  2. B "105"
  3. C NaN
  4. 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);

  1. A Evaluates each expression and returns the LAST one - x is 3
  2. B Creates an array
  3. C SyntaxError
  4. 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;

  1. A Removes the property from the object
  2. B Deletes the whole object
  3. C Sets the property to null
  4. 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
  1. A Whether the object has a property with that KEY
  2. B Whether a value exists in the object
  3. C Whether a string contains 'name'
  4. 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
  1. A "53" - the number is converted to a string and concatenated
  2. B 8
  3. C NaN
  4. 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
  1. A 2 - unlike +, the minus operator converts strings to NUMBERS
  2. B "53"
  3. C "2"
  4. 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
  1. A "hello" - || returns the first TRUTHY value, not just true/false
  2. B true
  3. C 0
  4. 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");

  1. A "bye" - && returns the LAST value if all are truthy (or the first falsy one)
  2. B "hi"
  3. C true
  4. 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
  1. A || falls back on ANY falsy value (0, "", false); ?? falls back only on null or undefined
  2. B They are identical
  3. C ?? is deprecated
  4. 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
  1. A Converts any value to its boolean equivalent - here true
  2. B Nothing - it cancels out
  3. C Returns the string
  4. 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);

  1. A true false - chaining does NOT work as maths; the second becomes (3>2)=true, then true>1 is false
  2. B true true
  3. C false false
  4. 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
  1. A "boolean"
  2. B "number"
  3. C "string"
  4. 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)
  1. A Operating on the individual bits of numbers - flags, masks, low-level maths
  2. B Logical AND/OR only
  3. C String manipulation
  4. 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

Practice β€œOperators” as a quiz