JavaScript Quiz
Questions: 16 · 10 minutes
1. What does typeof null evaluate to in JavaScript?
""object""
""undefined""
""null""
""boolean""
2. What happens when this code runs: if (true) { let message = ""hello""; } console.log(message);?
It prints ""hello"" because an if block does not create a scope.
A ReferenceError occurs because message is block-scoped.
It prints undefined because message exists but has no value outside the block.
A SyntaxError occurs because let cannot be used inside an if statement.
3. Given const user = {}; what is the result of user.profile?.name?
A TypeError because profile does not exist
null because optional chaining supplies a null value
An empty string because name has not been assigned
undefined because optional chaining stops at the missing property
4. What does [2, 4, 5].every(n => n % 2 === 0) return?
[2, 4]
true, because at least one element is even
5, because that is the first element that fails the test
false, because not every element is even
5. What string is produced by JSON.stringify({ a: 1, b: undefined })?
{""a"":1}
{""a"":1,""b"":undefined}
{""a"":1,""b"":null}
The call returns undefined because one property is undefined.
6. What is produced by [...new Set([1, 1, 2, 2])]?
A four-item array containing 1, 1, 2, and 2
A two-item array containing 2 and 2
A Set object containing the numeric values 1 and 2
A two-item array containing the numeric values 1 and 2
7. If an async function contains return 5;, what does calling that function return?
A Promise fulfilled with the number 5
undefined until the function is awaited
A callback that supplies the number 5
The number 5 immediately, just like a regular function
8. What is the output order of this code: console.log(""A""); setTimeout(() => console.log(""B""), 0); Promise.resolve().then(() => console.log(""C"")); console.log(""D"");?
A, D, C, B
A, D, B, C
A, B, C, D
A, C, D, B
9. What happens after this code runs: const items = [1, 2]; items.push(3);?
It throws an error because a const array cannot be modified.
The array becomes [1, 2, 3] because const prevents rebinding, not mutation of the array.
A new array [1, 2, 3] is created, while items remains [1, 2].
The value 3 is ignored because push only works with arrays declared using let.
10. Consider: const obj = { value: 7, getValue() { const read = () => this.value; return read(); } }; What does obj.getValue() return?
The read function itself rather than its result
undefined, because arrow functions always set this to undefined
7, because the arrow function uses this from getValue
A TypeError because this cannot be used inside a nested function
11. What does Array.prototype.map() normally return?
The original array after replacing each of its elements
One accumulated value calculated from all elements
A new array containing only the elements that pass a test
A new array containing the result of applying a function to each element
12. What value is assigned to limit by const limit = 0 ?? 10;?
10, because 0 is falsy
null, because neither operand is null
0, because nullish coalescing falls back only for null or undefined
undefined, because ?? cannot be used with numeric values
13. After const [first, , third] = [10, 20, 30];, what values do first and third hold?
first is 10 and third is 20
first is 10 and third is 30
first is 20 and third is 30
Both are undefined because an array position was skipped.
14. Consider: function makeCounter() { let n = 0; return () => ++n; } const count = makeCounter(); console.log(count(), count()); What is printed?
0 0
1 2
1 1
0 1
15. What is the main difference between JavaScript's strict equality operator (===) and loose equality operator (==)?
Strict equality compares object contents, while loose equality compares object references.
Strict equality works only with numbers, while loose equality works with all data types.
Strict equality compares values without type coercion, while loose equality may convert types first.
Strict equality is used for assignment, while loose equality is used for comparison.
16. Given const letters = [""a"", ""b"", ""c""]; const removed = letters.splice(1, 1, ""x"", ""y"");, what are letters and removed afterward?
letters is [""a"", ""b"", ""c""] and removed is [""x"", ""y""]
letters is [""a"", ""x"", ""y""] and removed is [""b"", ""c""]
letters is [""a"", ""x"", ""y"", ""c""] and removed is [""b""]
letters is [""a"", ""c"", ""x"", ""y""] and removed is ""b""