Coding Solution

JavaScript MCQ test: 12 questions with answers (CodinGame, SkillValue type)

MCQJavaScript12 questionsUpdated

This quiz is for developers preparing for a JavaScript hiring test (SkillValue, CodinGame, HackerRank) or a front-end / Node.js interview. Every question comes with a full explanation of the right answer and of the traps hidden behind the wrong ones.

In short

JavaScript hiring tests spend very little time on frameworks and a lot of time on the language itself. The topics that come up most often on SkillValue, CodinGame and HackerRank are var/let/const hoisting, closures inside loops, the value of this depending on how a function is called, type coercion with ==, and the execution order of setTimeout, promises and async/await. Array methods (map, filter, reduce, sort) and copying objects by reference follow closely. Most questions are code reading: you are given ten lines and asked to predict the output.

The topics that come up most

Practise: 12 questions with answers

Pick an answer to see the explanation.

What does this program print?
console.log(x);
var x = 5;
console.log(x);

var declarations are hoisted to the top of their function or script, but only the declaration moves: the assignment stays where it is. At the first console.log, x already exists and holds undefined; at the second it holds 5. There is no ReferenceError (that happens with let or const), and null is never a default value in JavaScript.

What does this program print?
let a = 1;
{
  console.log(a);
  let a = 2;
}

The block declares its own variable a with let. That declaration is known from the very start of the block, but the variable sits in the temporal dead zone (TDZ) until the line let a = 2 runs: reading it before that throws a ReferenceError. The outer a holding 1 is shadowed by the inner declaration, so 1 is never printed; undefined would be the behavior of var, not let.

What is the value of this expression?
1 + "2" - 1

Evaluation goes left to right. 1 + "2": as soon as one operand is a string, + concatenates and yields the string "12". Then "12" - 1: the - operator only works on numbers, so it converts "12" to 12 and returns the number 11. The result is a number, not a string, and there is no NaN because "12" converts cleanly.

What does this program print?
const nums = [1, 10, 2, 21];
nums.sort();
console.log(nums);

Without a compare function, sort() converts each element to a string and compares strings character by character (UTF-16 order). "1" < "10" < "2" < "21", so the array is already in order and does not move. To sort numerically you must write nums.sort((a, b) => a - b). The comparator is optional, so no error is thrown.

What does this program print?
for (var i = 0; i < 3; i++) {
  setTimeout(() => console.log(i), 0);
}

var is function-scoped: there is a single variable i shared by all three callbacks. The setTimeout callbacks run after the loop has finished, when i is 3 (the condition i < 3 just became false), so each prints 3. With let i, every iteration would get its own binding and the output would be 0 1 2.

What does this program print?
console.log([] == false, null == undefined, null == 0, NaN == NaN);

[] == false: both sides are converted to numbers, [] becomes "" then 0 and false becomes 0, hence true. null == undefined is a special case in the specification: true. However null is only equal to null and undefined, never to 0: false. Finally NaN is not equal to anything, not even itself: false. These rules are exactly why === is recommended.

What does this program print?
const words = ["a", "bb", "a", "ccc", "bb", "a"];
const count = words.reduce((acc, w) => {
  acc[w] = (acc[w] || 0) + 1;
  return acc;
}, {});
console.log(count.a, count.bb, Object.keys(count).length);

reduce starts from the empty object passed as the initial value and, for each word, increments its counter (acc[w] || 0 handles the first occurrence). The callback returns acc on every pass, so the accumulator is never lost. "a" appears 3 times, "bb" 2 times, and there are 3 distinct keys (a, bb, ccc). The answer 3 2 6 confuses the number of keys with the number of array elements.

What does this program print?
const a = { x: 1, nested: { y: 2 } };
const b = { ...a };
b.x = 10;
b.nested.y = 20;
console.log(a.x, a.nested.y);

Spread creates a new object and copies first-level properties. x is a primitive, so b.x = 10 does not affect a.x, which stays 1. But nested is a reference to the same inner object: a.nested and b.nested point to the same { y: 2 }, so changing b.nested.y also changes a.nested.y, which becomes 20. A deep copy requires structuredClone or a recursive copy.

What does this program print?
class Counter {
  constructor() { this.count = 0; }
  inc = () => { this.count++; return this.count; };
  dec() { this.count--; return this.count; }
}
const c = new Counter();
const inc = c.inc;
const dec = c.dec;
console.log(inc());
console.log(dec());

inc is a class field defined with an arrow function: its this is captured lexically when the instance is built and always points to that instance, even when inc() is called detached. So it prints 1. dec is a regular method: called on its own, its this is undefined (class bodies are strict mode), and this.count-- throws a TypeError. This is exactly why arrow functions or bind are used for event handlers.

In what order are the letters printed?
console.log("A");
setTimeout(() => console.log("B"), 0);
Promise.resolve().then(() => console.log("C"));
(async () => {
  console.log("D");
  await null;
  console.log("E");
})();
console.log("F");

All synchronous code runs first: A, then D (the start of an async function runs synchronously up to the first await), then F. The microtask queue is then drained in registration order: C (then) was queued before E (resumption after await), so C then E. Only after that does the setTimeout macrotask print B, even with a 0 ms delay. Any answer placing B before C or E ignores microtask priority.

What does this program print?
async function run() {
  const ids = [1, 2];
  ids.forEach(async (id) => {
    await null;
    console.log("item", id);
  });
  console.log("done");
}
run();

forEach ignores its callback's return value, so it does not wait for the promises returned by the async functions. Each callback runs up to its await and yields, forEach finishes, and "done" is printed immediately. The resumptions after await are microtasks that then run in order: item 1, then item 2. To wait for each iteration you need a for...of loop with await, or Promise.all over a map. The code is syntactically valid because await is inside an async function.

What does this program print?
console.log(typeof NaN, NaN === NaN, Object.is(NaN, NaN), [NaN].includes(NaN), [NaN].indexOf(NaN));

NaN (Not a Number) is, paradoxically, of type "number": it is a special IEEE 754 numeric value. NaN === NaN is false because NaN equals nothing, including itself. Object.is was designed to fix that case and returns true. includes uses the SameValueZero algorithm, which treats NaN as equal to NaN: true. indexOf uses === internally and therefore never finds NaN: -1. To test for NaN, use Number.isNaN.

On test day

  1. For every "what does this print" question, trace execution line by line on paper with three columns: synchronous, microtasks, macrotasks. The final order follows mechanically.
  2. Whenever you see var inside a loop or a method pulled off its object (const f = obj.method), look for the trap: the question is almost always about scope or this.
  3. When facing a == comparison, convert both sides to numbers by hand, except for null and undefined, which are only equal to each other.
  4. Double-check array methods every time: sort() without a comparator, forEach ignoring promises, map returning a new array while push mutates the original. These three are among the most frequent sources of wrong answers.

Practise with Coding Solutions

On a practice MCQ shown on screen, the Coding Solutions extension explains the right answer and the rule behind it, in 29 languages. For coding exercises, see also our explained CodinGame solutions and the platform guides.

Frequently asked questions

What level of JavaScript is expected in a hiring test?

Most tests target an intermediate level: understanding scope, closures, this, promises and array methods. Framework questions (React, Vue) are usually in a separate test. A junior developer who is solid on these fundamentals does well.

Do tests cover ES6+ or old-style JavaScript?

Both. Platforms deliberately ask questions with var, function and == to check that you know the legacy behaviors, since they are still present in many codebases. You need let/const, arrow functions, spread and async/await just as much as the historical mechanics.

How long is a JavaScript MCQ on SkillValue or CodinGame?

Usually 15 to 30 minutes for 15 to 25 questions, roughly one minute per question. Code-reading questions take longer: answer the quick theory questions first, then come back to the code snippets.

How should I practice before the test?

Redo this quiz until you can justify every answer without looking at the explanation, then write your own variants and run them in the browser console or in Node to confirm. Seeing the real output fixes wrong intuitions far better than rereading a tutorial.

Other languages

★ 5,0 · 16 avis · 928 utilisateurs Add to Chrome