Free JavaScript quiz
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
console.log("5" + 2, "5" - 2);Type coercion
1 / 10
What does this code log?
0 correct · Keys 1 to 4 to answer
Your score and streak are saved in this browser. No account needed.
Question bank
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 types1Type coercionconsole.log("5" + 2, "5" - 2);
What does this code log?
console.log("5" + 2, "5" - 2);- A7 3
- B52 NaN
- C52 3
- 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);- Atrue false true
- Bfalse false false
- Ctrue true true
- 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 JavaScript3typeofconsole.log(typeof null, typeof [], typeof NaN);
What does this code log?
console.log(typeof null, typeof [], typeof NaN);- Anull array NaN
- Bnull object number
- Cobject array number
- 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 JavaScript4Template literalsconsole.log(`${n} items cost ${n * 2}`);
What does this code log?
const n = 3;console.log(`${n} items cost ${n * 2}`);- A${n} items cost ${n * 2}
- B3 items cost 6
- C3 items cost 32
- 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 JavaScript5Truthy and falsyconsole.log(Boolean("0"), Boolean(""), Boolean([]));
What does this code log?
console.log(Boolean("0"), Boolean(""), Boolean([]));- Atrue false true
- Bfalse false false
- Ctrue false false
- 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);- AAna
- Bundefined
- CBen
- 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 const7Array lengthconsole.log(a.length);
What does this code log?
const a = [1, 2, 3];a[5] = 6;console.log(a.length);- A4
- B6
- C3
- 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));- Atrue true
- Btrue false
- Cfalse false
- 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 JavaScript9filter 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));- A[ 20, 40 ]
- B[ 10, 20, 30, 40 ]
- C[ 10, 30 ]
- 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 JavaScript10Number precisionconsole.log(0.1 + 0.2 === 0.3);
What does this code log?
console.log(0.1 + 0.2 === 0.3);- Afalse
- Btrue
- Cundefined
- 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 JavaScriptCore
Collections, scope and errors11Hoistingvar a = 5;
What does this code log?
console.log(a);var a = 5;- A5
- BIt throws ReferenceError
- Cnull
- 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 const12Block scopeconsole.log(x);
What does this code log?
let x = 1;{ let x = 2;}console.log(x);- A2
- B1
- Cundefined
- 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 JavaScript13Sortingconsole.log([10, 9, 1].sort());
What does this code log?
console.log([10, 9, 1].sort());- A[ 1, 9, 10 ]
- B[ 9, 10, 1 ]
- C[ 10, 9, 1 ]
- 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);- A[ 2, 3 ] [ 2, 3 ] [ 1, 4 ]
- B[ 2, 3 ] [ 2, 3 ] [ 1, 2, 3, 4 ]
- C[ 2, 3, 4 ] [ 2, 3 ] [ 1, 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 splice15reduceconsole.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));- A6
- B10123
- C16
- 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({} === {}, [] == []);- Atrue true
- Bfalse false
- Ctrue false
- 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 JavaScript17Copying 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);- A1
- B0
- C2
- 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 spread18for...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);- A0, then 1
- Ba, then b
- Cundefined twice
- 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 forEach19Destructuring defaultsconsole.log(a, b);
What does this code log?
const { a = 1, b = 2 } = { a: undefined, b: null };console.log(a, b);- Aundefined null
- Bundefined 2
- C1 2
- 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 JavaScript20Optional chainingconsole.log(user.profile?.name ?? "guest");
What does this code log?
const user = { profile: null };console.log(user.profile?.name ?? "guest");- Aguest
- Bnull
- Cundefined
- 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 JavaScriptTricky
The ones interviews love21Closures 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(" "));- A0 1 2
- B3 3 3
- C2 2 2
- 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 closures22The 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");- AA B C D
- BA C D B
- CA D B C
- 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 loop23await 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);- A1 2 3
- B3 1 2
- C1 3 2
- 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 macrotasks24this 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());- A1 1
- B1 undefined
- Cundefined 1
- 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 functions25async functionsconsole.log(result instanceof Promise);
What does this code log?
async function getValue() { return 42;}const result = getValue();console.log(result instanceof Promise);- Atrue
- Bfalse
- C42
- 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/await26map with parseIntconsole.log(["1", "2", "3"].map(parseInt));
What does this code log?
console.log(["1", "2", "3"].map(parseInt));- A[ 1, 2, 3 ]
- B[ NaN, NaN, NaN ]
- C[ 1, NaN, NaN ]
- 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());- A3 3
- B2 1
- C1 1
- 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 closures28Mixed operatorsconsole.log(1 + "2" - 1, "3" * "4", true + 1);
What does this code log?
console.log(1 + "2" - 1, "3" * "4", true + 1);- A2 12 2
- B11 12 2
- C121 34 true1
- 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));- Axy
- Bx
- Cundefined
- 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 JavaScript30Function hoistingvar g = () => {};
What does this code log?
console.log(typeof f, typeof g);function f() {}var g = () => {};- Afunction function
- BIt throws ReferenceError
- Cundefined undefined
- 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 constKeep going
What to do after this quiz
FAQ
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.