Types & Coercion Questions with Answers

20 practice questions on Types & Coercion (JavaScript) with answers and step-by-step solutions. Try 10 samples below, then practise all 20 free in the AptiNest app.

Sample questions

Question 1Easy

What is the output?

console.log(null == undefined, null === undefined);
  1. true, false
  2. false, false
  3. true, true
  4. false, true

Answer: A. true, false

Explanation

Loose == treats null and undefined as equal to each other, but === sees different types.

Step-by-step solution

  1. null == undefined is a special-case true.
  2. Strict equality needs the same type, so false.

Why the other options are wrong

  • B. Loose equality treats null and undefined as equal.
  • C. Strict equality compares types too.
  • D. Reversed results.

Question 2Easy

What is the output?

console.log(Number('12px'), parseInt('12px'));
  1. 12, 12
  2. NaN, NaN
  3. NaN, 12
  4. 12, NaN

Answer: C. NaN, 12

Explanation

Number() needs the whole string to be numeric so gives NaN; parseInt parses the leading digits and gives 12.

Step-by-step solution

  1. Number("12px") is NaN.
  2. parseInt stops at "p" and returns 12.

Why the other options are wrong

  • A. Number() rejects trailing garbage.
  • B. parseInt reads the leading digits.
  • D. The roles are reversed.

Question 3Easy

What is the output?

console.log(NaN === NaN, Object.is(NaN, NaN));
  1. false, true
  2. true, true
  3. false, false
  4. true, false

Answer: A. false, true

Explanation

NaN is never === to anything, including itself, but Object.is considers NaN identical to NaN.

Step-by-step solution

  1. === follows IEEE rules: NaN !== NaN.
  2. Object.is uses SameValue, so NaN matches NaN.

Why the other options are wrong

  • B. NaN is not strictly equal to itself.
  • C. Object.is treats NaN as the same value as NaN.
  • D. The results are the other way round.

Tip: Use Number.isNaN(x) to test for NaN.

Question 4Easy

What is the output?

console.log([] + []);console.log(typeof ([] + []));
  1. string
  2. 0, number
  3. [], object
  4. undefined, undefined

Answer: A. string

Explanation

Each empty array converts to the empty string, so the sum is "" of type string.

Step-by-step solution

  1. [] becomes "" through toString.
  2. "" + "" is "".
  3. typeof "" is "string".

Why the other options are wrong

  • B. Arrays convert to strings, not numbers, with +.
  • C. The result is a primitive string.
  • D. The expression is well defined.

Question 5Medium

What is the output?

console.log([1, 2, 3] + [4]);
  1. 1,2,34
  2. 1,2,3,4
  3. [1, 2, 3, 4]
  4. 10

Answer: A. 1,2,34

Explanation

+ converts both arrays to strings ("1,2,3" and "4") and concatenates them.

Step-by-step solution

  1. The + operator, when used on two arrays, first converts each array to a string.
  2. [1, 2, 3] becomes the string "1,2,3", and [4] becomes "4".
  3. These two strings are then concatenated, giving "1,2,34".

Why the other options are wrong

  • B. "1,2,3,4" would require + to somehow merge the arrays' contents with a comma between the last elements, but + just concatenates the two already-stringified arrays directly.
  • C. [1, 2, 3, 4] would be the result of using the array's .concat() method instead of +, which does something entirely different.
  • D. 10 would only make sense if + tried to add array lengths or something similarly unrelated to how arrays stringify.

Tip: To actually combine arrays, use arr1.concat(arr2) or the spread syntax [...arr1, ...arr2], never +.

Question 6Medium

What is the output?

console.log(String(123) + 1, Number('1' + 1), '2' * '3');
  1. 1231, 12, 6
  2. 124, 11, 6
  3. 1231, 11, 6
  4. 1231, 11, 5

Answer: C. 1231, 11, 6

Explanation

String(123)+1 concatenates to "1231", Number("11") is 11, and * coerces to numbers giving 6.

Step-by-step solution

  1. String(123) + 1 is "1231".
  2. Number("1" + 1) = Number("11") = 11.
  3. "2" * "3" = 6.

Why the other options are wrong

  • A. "1" + 1 is "11", so Number gives 11, not 12.
  • B. String(123) is a string so + concatenates.
  • D. "2" * "3" multiplies to 6.

Question 7Medium

What is the output?

console.log(+'', +' ', +'\n', +[], +{});
  1. 0, 0, 0, 0, 0
  2. 0, 0, 0, 0, NaN
  3. NaN, NaN, NaN, 0, NaN
  4. 0, 0, 0, NaN, NaN

Answer: B. 0, 0, 0, 0, NaN

Explanation

Empty or whitespace-only strings and the empty array convert to 0; a plain object becomes "[object Object]" and then NaN.

Step-by-step solution

  1. Whitespace-only strings trim to empty, which is 0.
  2. +[] goes via "" to 0.
  3. +{} goes via "[object Object]" to NaN.

Why the other options are wrong

  • A. An object converts to NaN.
  • C. Blank or whitespace strings convert to 0.
  • D. An empty array becomes "" and then 0.

Question 8Medium

What is the output?

console.log(typeof NaN, typeof [], typeof function(){}, typeof Symbol());
  1. number, array, function, symbol
  2. number, object, function, symbol
  3. number, object, object, symbol
  4. NaN, object, function, symbol

Answer: B. number, object, function, symbol

Explanation

NaN is a number, arrays are objects, functions report "function", symbols report "symbol".

Step-by-step solution

  1. typeof NaN is "number".
  2. Arrays are objects.
  3. Functions give "function", symbols "symbol".

Why the other options are wrong

  • A. typeof has no "array" result.
  • C. Functions report "function".
  • D. typeof NaN is "number".

Tip: Use Array.isArray to detect arrays.

Question 9Medium

What is the output?

console.log(1 < 2 < 3, 3 > 2 > 1);
  1. true, false
  2. true, true
  3. false, false
  4. false, true

Answer: A. true, false

Explanation

Comparisons evaluate left to right: (1<2)<3 is true<3 -> 1<3 true; (3>2)>1 is true>1 -> 1>1 false.

Step-by-step solution

  1. (1 < 2) is true.
  2. true < 3 becomes 1 < 3 = true.
  3. (3 > 2) is true, true > 1 is 1 > 1 = false.

Why the other options are wrong

  • B. 3 > 2 gives true, which becomes 1; 1 > 1 is false.
  • C. 1 < 2 < 3 becomes true < 3, i.e. 1 < 3.
  • D. The results are reversed.

Question 10Hard

What is the output?

console.log(1 < 2 < 3, 3 > 2 > 1);
  1. true true
  2. false true
  3. true false
  4. false false

Answer: C. true false

Explanation

Comparisons are evaluated left to right. 1 < 2 is true, and true < 3 becomes 1 < 3 (true). 3 > 2 is true, and true > 1 becomes 1 > 1 (false).

Step-by-step solution

  1. Comparison operators like < and > are evaluated strictly left to right, with no special 'chaining' behaviour like in math.
  2. For 1 < 2 < 3: first 1 < 2 evaluates to true, then true < 3 is evaluated, where true converts to 1, so 1 < 3 is true.
  3. For 3 > 2 > 1: first 3 > 2 evaluates to true, then true > 1 is evaluated, where true converts to 1, so 1 > 1 is false.

Why the other options are wrong

  • A. true true would mean both expressions behave like mathematical chained inequalities, but JavaScript doesn't support that; it evaluates strictly pairwise.
  • B. false true reverses which expression is actually true and which is false.
  • D. false false would mean both end up false, but the first expression (1 < 2 < 3) does evaluate to true.

Tip: Never chain comparisons like a < b < c in JavaScript expecting mathematical range-checking - write (a < b) && (b < c) instead.

Practise all 20 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all JavaScript topics · All topics