JavaScript is a dynamic, versatile language that powers the web. But with its quirks and idiosyncrasies, it can sometimes produce unexpected results. Dive into this quiz to challenge your JavaScript knowledge and learn some of its tricky parts!
1. What will the following code output?
console.log(0.1 + 0.2 === 0.3);
javascript
a) true
b) false
Answer:
b) false. Floating point arithmetic can lead to precision errors in JavaScript (and many other languages). 0.1 + 0.2 results in 0.30000000000000004, not exactly 0.3.
2. What is the output of the following?
console.log(typeof NaN);
a) number
b) NaN
c) undefined
d) object
Answer:
a) number. Even though it's "Not-a-Number", its type is still a number!
3. What will be printed on the console?
let a = {},
b = { key: 'b' },
c = { key: 'c' };
a[b] = 123;
a[c] = 456;
console.log(a[b]);
a) undefined
b) 123
c) 456
Answer:
c) 456. Both b and c objects are converted to the string '[object Object]' when used as object keys, so they overwrite each other.
4. What’s the output of the following:
console.log((2 + '2') - 2);
a) 22
b) 20
c) 4
d) 02
Answer:
b) 20. The addition operation between a number and a string results in string concatenation. The subtraction forces the string '22' to be converted back to a number.
5. Given the snippet below, what will be printed?
let arr = [10, 20, 30, 40, 50];
delete arr[3];
console.log(arr[3]);
a) 40
b) undefined
c) null
d) 50
Answer:
b) undefined. Using the delete operator on an array item will remove the item but will not shift the other items. The array slot becomes undefined.
6. What does the following log?
console.log('2' == 2);
console.log('2' === 2);
a) true true
b) true false
c) false true
d) false false
Answer:
b) true false. The double equals == performs type coercion and only checks for value equality. The triple equals === checks for both value and type, and since a string is not the same type as a number, it returns false.
7. What is the output of:
console.log(typeof typeof 1);
a) number
b) string
c) undefined
d) object
Answer:
b) string. The inner typeof 1 returns 'number'. Then, typeof 'number' returns 'string'.
I hope you enjoyed this quirky JavaScript quiz! JavaScript’s unique characteristics can sometimes catch even the most experienced developers off guard. Testing your knowledge and understanding these nuances can help you write better, bug-free code.
How did you do?
🔗 Connect with me on:
A Big Thank You! 🌟
Readers: Grateful for every click and read.
Engagers: Claps, comments, shares — you’re the best!
Followers: Honored by your continuous support.
Silent Admirers: Your presence doesn’t go unnoticed.
Constructive Critics: Your insights help us improve.
💌 Stay connected and thanks for being part of our journey.
Top comments (0)