JavaScript Practice Quiz.

Thirty JavaScript questions in three levels. Pick an answer and see at once whether you were right, and why.

  • 30 questions
  • 3 levels
  • No sign-in
  • Checked on Node.js 26
0 day streakBest –
JavaScript
console.log("5" + 2, "5" - 2);

1 / 10

What does this code log?

0 correct

Your score and streak are saved in this browser. No account needed.

All 30 JavaScript questions and answers

Prefer to read? Open any question to see the code, the answer and the reason behind it.

Basics

Syntax and everyday types
1Type coercionconsole.log("5" + 2, "5" - 2);

What does this code log?

console.log("5" + 2, "5" - 2);
  1. A7 3
  2. B52 NaN
  3. C52 3
  4. D7 NaN

Answer: C. With +, a string on either side means joining, so "5" + 2 is "52". The - operator only works on numbers, so "5" is converted and the result is 3.

2Equalityconsole.log(0 == "", 0 === "", null == undefined);

What does this code log?

console.log(0 == "", 0 === "", null == undefined);
  1. Atrue false true
  2. Bfalse false false
  3. Ctrue true true
  4. Dfalse false true

Answer: A. == converts both sides to a common type first, so 0 equals "" and null equals undefined. === does no conversion, and a number is never strictly equal to a string.

Double vs triple equals in JavaScript
3typeofconsole.log(typeof null, typeof [], typeof NaN);

What does this code log?

console.log(typeof null, typeof [], typeof NaN);
  1. Anull array NaN
  2. Bnull object number
  3. Cobject array number
  4. Dobject object number

Answer: D. typeof null is "object", a mistake kept from the first version of the language. Arrays are objects, so use Array.isArray(). NaN is a value of the number type.

The typeof operator in JavaScript
4Template literalsconsole.log(`${n} items cost ${n * 2}`);

What does this code log?

const n = 3;console.log(`${n} items cost ${n * 2}`);
  1. A${n} items cost ${n * 2}
  2. B3 items cost 6
  3. C3 items cost 32
  4. DIt throws SyntaxError

Answer: B. Inside backticks, ${ } runs the code between the braces and puts the result in the string. Single or double quotes would print the text as written.

Strings with backticks in JavaScript
5Truthy and falsyconsole.log(Boolean("0"), Boolean(""), Boolean([]));

What does this code log?

console.log(Boolean("0"), Boolean(""), Boolean([]));
  1. Atrue false true
  2. Bfalse false false
  3. Ctrue false false
  4. Dfalse false true

Answer: A. Only a handful of values are falsy: false, 0, "", null, undefined and NaN. A non-empty string such as "0" is truthy, and so is every object, including an empty array.

6const and objectsconsole.log(user.name);

What does this code log?

const user = { name: "Ana" };user.name = "Ben";console.log(user.name);
  1. AAna
  2. Bundefined
  3. CBen
  4. DIt throws TypeError

Answer: C. const stops the name user from being pointed at something else. It does not freeze the object. Changing a property is allowed; user = {} would throw.

var vs let vs const
7Array lengthconsole.log(a.length);

What does this code log?

const a = [1, 2, 3];a[5] = 6;console.log(a.length);
  1. A4
  2. B6
  3. C3
  4. DIt throws RangeError

Answer: B. length is always one more than the highest index. Writing to index 5 stretches the array to 6, leaving empty slots at indexes 3 and 4.

8NaNconsole.log(NaN === NaN, Number.isNaN(NaN));

What does this code log?

console.log(NaN === NaN, Number.isNaN(NaN));
  1. Atrue true
  2. Btrue false
  3. Cfalse false
  4. Dfalse true

Answer: D. NaN is the only value that is not equal to itself, so comparing with === never works. Use Number.isNaN() to test for it.

What is NaN in JavaScript
9filter and mapconsole.log(nums.filter((n) => n % 2).map((n) => n * 10));

What does this code log?

const nums = [1, 2, 3, 4];console.log(nums.filter((n) => n % 2).map((n) => n * 10));
  1. A[ 20, 40 ]
  2. B[ 10, 20, 30, 40 ]
  3. C[ 10, 30 ]
  4. D[ 1, 3 ]

Answer: C. n % 2 is 1 for odd numbers and 0 for even ones, and 0 is falsy, so filter keeps 1 and 3. map then multiplies each by 10.

Loop through arrays in JavaScript
10Number precisionconsole.log(0.1 + 0.2 === 0.3);

What does this code log?

console.log(0.1 + 0.2 === 0.3);
  1. Afalse
  2. Btrue
  3. Cundefined
  4. DIt throws an error

Answer: A. Numbers are stored in binary floating point, which cannot hold 0.1 or 0.2 exactly. The sum is 0.30000000000000004. Compare with a small tolerance, or work in whole units such as cents.

Rounding numbers in JavaScript

Core

Collections, scope and errors
11Hoistingvar a = 5;

What does this code log?

console.log(a);var a = 5;
  1. A5
  2. BIt throws ReferenceError
  3. Cnull
  4. Dundefined

Answer: D. A var declaration is moved to the top of its scope, but the assignment stays where it is. The variable exists and holds undefined when the log runs. With let or const the same code throws.

var vs let vs const
12Block scopeconsole.log(x);

What does this code log?

let x = 1;{  let x = 2;}console.log(x);
  1. A2
  2. B1
  3. Cundefined
  4. DIt throws SyntaxError

Answer: B. let is scoped to the nearest pair of braces. The inner x is a separate variable that disappears when the block ends.

Global vs local variables in JavaScript
13Sortingconsole.log([10, 9, 1].sort());

What does this code log?

console.log([10, 9, 1].sort());
  1. A[ 1, 9, 10 ]
  2. B[ 9, 10, 1 ]
  3. C[ 10, 9, 1 ]
  4. D[ 1, 10, 9 ]

Answer: D. With no compare function, sort() turns the items into strings and orders them as text, and "10" comes before "9". Pass (a, b) => a - b to sort numbers by value.

14slice and spliceconsole.log(b, c, a);

What does this code log?

const a = [1, 2, 3, 4];const b = a.slice(1, 3);const c = a.splice(1, 2);console.log(b, c, a);
  1. A[ 2, 3 ] [ 2, 3 ] [ 1, 4 ]
  2. B[ 2, 3 ] [ 2, 3 ] [ 1, 2, 3, 4 ]
  3. C[ 2, 3, 4 ] [ 2, 3 ] [ 1, 4 ]
  4. D[ 2, 3 ] [ 1, 4 ] [ 2, 3 ]

Answer: A. slice copies part of an array and leaves it untouched. splice removes items from the array itself and returns what it removed, so a is left with 1 and 4.

Array slice vs splice
15reduceconsole.log([1, 2, 3].reduce((sum, n) => sum + n, 10));

What does this code log?

console.log([1, 2, 3].reduce((sum, n) => sum + n, 10));
  1. A6
  2. B10123
  3. C16
  4. DNaN

Answer: C. The second argument is the starting value. reduce begins with 10 and adds 1, 2 and 3 to it.

16Comparing objectsconsole.log({} === {}, [] == []);

What does this code log?

console.log({} === {}, [] == []);
  1. Atrue true
  2. Bfalse false
  3. Ctrue false
  4. Dfalse true

Answer: B. Objects and arrays are compared by reference, with == as well as ===. Each literal creates a new object, so two of them are never equal, even when they look the same.

Double vs triple equals in JavaScript
17Copying objectsconsole.log(a.tags.length);

What does this code log?

const a = { tags: ["x"] };const b = { ...a };b.tags.push("y");console.log(a.tags.length);
  1. A1
  2. B0
  3. C2
  4. DIt throws TypeError

Answer: C. Spread makes a shallow copy: the top level is new, but nested objects and arrays are shared. a.tags and b.tags are the same array. Use structuredClone(a) for a full copy.

Copy nested objects with spread
18for...infor (const i in arr) console.log(i);

What does this loop log?

const arr = ["a", "b"];for (const i in arr) console.log(i);
  1. A0, then 1
  2. Ba, then b
  3. Cundefined twice
  4. DIt throws TypeError

Answer: A. for...in walks over the keys, which for an array are the indexes (as strings). for...of is the loop that gives you the values.

for, for...of and forEach
19Destructuring defaultsconsole.log(a, b);

What does this code log?

const { a = 1, b = 2 } = { a: undefined, b: null };console.log(a, b);
  1. Aundefined null
  2. Bundefined 2
  3. C1 2
  4. D1 null

Answer: D. A default is used only when the value is undefined. a is undefined, so it becomes 1. null is a real value, so b stays null.

Default parameters in JavaScript
20Optional chainingconsole.log(user.profile?.name ?? "guest");

What does this code log?

const user = { profile: null };console.log(user.profile?.name ?? "guest");
  1. Aguest
  2. Bnull
  3. Cundefined
  4. DIt throws TypeError

Answer: A. ?. stops and gives undefined when the thing before it is null or undefined, instead of throwing. ?? then supplies "guest" because the left side is undefined.

Optional chaining in JavaScript

Tricky

The ones interviews love
21Closures in loopsconsole.log(fns.map((fn) => fn()).join(" "));

What does this code log?

const fns = [];for (var i = 0; i < 3; i++) {  fns.push(() => i);}console.log(fns.map((fn) => fn()).join(" "));
  1. A0 1 2
  2. B3 3 3
  3. C2 2 2
  4. D0 0 0

Answer: B. var gives the whole loop one shared i. All three functions read that same variable, and by the time they run the loop has ended with i at 3. let gives each pass its own i.

JavaScript closures
22The event loopconsole.log("D");

In what order are the letters logged?

console.log("A");setTimeout(() => console.log("B"), 0);Promise.resolve().then(() => console.log("C"));console.log("D");
  1. AA B C D
  2. BA C D B
  3. CA D B C
  4. DA D C B

Answer: D. The plain statements run first: A, then D. Promise callbacks are microtasks and run as soon as the current code finishes, so C is next. The timer callback is a separate task and runs last.

The JavaScript event loop
23await orderconsole.log(3);

In what order are the numbers logged?

async function run() {  console.log(1);  await null;  console.log(2);}run();console.log(3);
  1. A1 2 3
  2. B3 1 2
  3. C1 3 2
  4. D2 1 3

Answer: C. An async function runs like normal code up to its first await. There it pauses and hands control back, so 3 is logged. The rest continues as a microtask afterwards.

Microtasks vs macrotasks
24this in functionsconsole.log(counter.regular(), counter.arrow());

What does this code log?

const counter = {  count: 1,  regular() { return this.count; },  arrow: () => this?.count,};console.log(counter.regular(), counter.arrow());
  1. A1 1
  2. B1 undefined
  3. Cundefined 1
  4. DIt throws TypeError

Answer: B. A regular method gets the object before the dot as this, so regular() returns 1. An arrow function has no this of its own: it uses the one from the surrounding code, which is not counter.

JavaScript arrow functions
25async functionsconsole.log(result instanceof Promise);

What does this code log?

async function getValue() {  return 42;}const result = getValue();console.log(result instanceof Promise);
  1. Atrue
  2. Bfalse
  3. C42
  4. Dundefined

Answer: A. An async function always returns a promise, even when the body returns a plain value. Use await getValue() to get the 42 out of it.

Promises to async/await
26map with parseIntconsole.log(["1", "2", "3"].map(parseInt));

What does this code log?

console.log(["1", "2", "3"].map(parseInt));
  1. A[ 1, 2, 3 ]
  2. B[ NaN, NaN, NaN ]
  3. C[ 1, NaN, NaN ]
  4. D[ 1, 2, NaN ]

Answer: C. map passes the item and its index. parseInt takes the index as the number base: parseInt("1", 0) is 1, but "2" in base 1 and "3" in base 2 are not valid. Use map(Number).

27Closures keep stateconsole.log(a(), b());

What does this code log?

function counter() {  let n = 0;  return () => ++n;}const a = counter();const b = counter();a(); a();console.log(a(), b());
  1. A3 3
  2. B2 1
  3. C1 1
  4. D3 1

Answer: D. Every call to counter() creates a new n, and the returned function keeps hold of its own one. a has been called three times; b only once.

JavaScript closures
28Mixed operatorsconsole.log(1 + "2" - 1, "3" * "4", true + 1);

What does this code log?

console.log(1 + "2" - 1, "3" * "4", true + 1);
  1. A2 12 2
  2. B11 12 2
  3. C121 34 true1
  4. D11 NaN 2

Answer: B. 1 + "2" joins to "12", then - converts it back to a number: 12 - 1 is 11. * always converts to numbers, giving 12. true becomes 1 in arithmetic.

29Promise chains .then((v) => console.log(v));

What does this code log?

Promise.reject("x")  .catch((e) => e + "y")  .then((v) => console.log(v));
  1. Axy
  2. Bx
  3. Cundefined
  4. DNothing: the error is never handled

Answer: A. catch handles the rejection, and whatever it returns becomes the value of a resolved promise. The chain carries on, so then receives "xy".

Error handling in JavaScript
30Function hoistingvar g = () => {};

What does this code log?

console.log(typeof f, typeof g);function f() {}var g = () => {};
  1. Afunction function
  2. BIt throws ReferenceError
  3. Cundefined undefined
  4. Dfunction undefined

Answer: D. A function declaration is hoisted whole, so f is ready before its line. g is a var: the name is hoisted but the arrow function is assigned only when that line runs.

var vs let vs const

Questions people ask

Is this JavaScript quiz good for interview practice?

Yes. Coercion, equality, hoisting, closures, shallow copies and the event loop are what JavaScript interviews return to again and again. The Tricky level is built from those questions.

Where were the answers checked?

Each snippet was run on Node.js 26 and the marked answer matches what it logged. The behaviour is part of the language, so browsers give the same results; only the way arrays are printed differs slightly.

Is my score saved?

Yes, in your browser on this device. Your best score for each level, your day streak and the question you stopped at are all there when you come back. Nothing is sent to a server and you do not need an account.

How do I get more JavaScript questions?

Open the AI Quiz Generator on Rune Learn, type a topic such as "JavaScript promises" or "array methods", and it writes a fresh quiz with explanations.