10 JavaScript Quizs to Test Your Knowledge of This Language
November 1, 2024
JavaScript Quiz recently hit #2 on Product Hunt. It's a simple but clever tool: it gives you 10 random JavaScript questions to test how well you understand the language.
Every time you play, you get different questions. Here are some fascinating ones we encountered, along with detailed explanations that make sense of JavaScript's peculiarities.
Question 1
console.log(018 - 015);
Question 2
console.log(typeof typeof 1);
Question 3
console.log(0.1 + 0.2 == 0.3);
Question 4
const numbers = [33, 2, 8];
numbers.sort();
console.log(numbers[1]);
Question 5
console.log(false == "0");
Question 6
let array = [1, 2, 3];
array[6] = 9;
console.log(array[5]);
Question 7
console.log(typeof NaN);
Question 8
console.log(("b" + "a" + +"a" + "a").toLowerCase());
Question 9
const isTrue = true == [];
const isFalse = true == ![];
console.log(isTrue + isFalse);
Question 10
console.log("This is a string." instanceof String);
Answers
- Question 1:
5 - Question 2:
string - Question 3:
false - Question 4:
33 - Question 5:
true - Question 6:
undefined - Question 7:
number - Question 8:
'banana' - Question 9:
0 - Question 10:
false
The Interesting Parts Explained
Question 1: The Legacy of Octal Numbers
Here's something weird: in JavaScript, when you write a number starting with 0, you're accidentally using octal (base-8) notation - a legacy feature from C. So 018 becomes decimal 18 (because 18 isn't valid in octal), while 015 becomes decimal 13 (1*8 + 5 = 13). Thus, 18 - 13 = 5. This is why modern JavaScript strongly discourages leading zeros in numbers.
Question 2: Types About Types
This is a nested type check. First, typeof 1 gives us 'number' (as a string). Then we check the type of that string with another typeof, giving us 'string'.
Question 3: The Famous Floating-Point Math Problem
This is probably the most famous JavaScript quirk. Due to how computers store decimal numbers in binary (base-2), 0.1 + 0.2 actually gives you something like 0.30000000000000004.
This isn't a JavaScript bug - it's how floating-point math works in most programming languages. For more details, see Why does 0.1 + 0.2 behave strangely in JavaScript?.
Question 4: The String-Based Sort Trap
The trap here is that JavaScript's default sort() converts everything to strings first. So [33, 2, 8] becomes ["33", "2", "8"], and then sorts alphabetically: ["2", "33", "8"]. That's why we get 33 as the middle number.
This surprises even experienced developers. For more on sorting, check out Understanding JavaScript's sort function.
Question 5: The Equality Conversion Dance
JavaScript's == operator performs type conversion before comparison. In this case, both false and '0' get converted to numbers: Number(false) is 0, and Number('0') is also 0. Hence, true.
This is why most JavaScript style guides recommend using === instead, which doesn't do type conversion. More details in The differences between ==, === and Object.is() in JavaScript.
Question 6: Arrays and Their Holes
The result is undefined. Assigning a value to array[6] sets indices 3, 4, and 5 to undefined. Hence, array[5] is undefined.
[1, 2, 3, undefined, undefined, undefined, 9];
Question 7: NaN is a Number (Really!)
One of JavaScript's most counterintuitive aspects: NaN (Not a Number) is actually of type number. This makes sense when you think about how numeric operations like parseFloat need to return something of type number even when the input is invalid. It's like having a special "error" number to represent failed calculations.
Question 8: The Accidental Banana
This is a delightful accident. Let's break it down:
'b' + 'a'gives'ba'+ 'a'tries to convert'a'to a number, fails, and givesNaN- So we get
'ba' + NaN + 'a'='baNaNa' toLowerCase()gives us'banana'
Question 9: Type Coercion Chaos
This shows how JavaScript's type coercion can be confusing:
true == []becomesfalsebecause they're converted to numbers:1 == 0true == ![]becomesfalsebecause![]isfalse- Adding two
falsevalues gives0becausefalsebecomes0when used in math
Question 10: Primitives vs Objects
The key insight here is that JavaScript has two different ways to represent strings:
- String primitives:
"hello" - String objects:
new String("hello")
instanceof only works with objects, not primitives. That's why "This is a string." instanceof String is false, while new String("This is a string.") instanceof String would be true.