JavaScript Guess the Output #1

Select one option

1 / 11

console.log(!![])

TL;DR answer sheet

JavaScript Guess the Output #1 — quiz answer sheet, click to expand.

1. console.log(!![])

Answer: true
Explanation: An empty array is a truthy value in JavaScript. The double negation converts it to a boolean, resulting in true.

2. console.log(0.1 + 0.2 === 0.3);

Answer: false
Explanation: Due to floating-point precision issues in JavaScript, 0.1 + 0.2 does not equal 0.3 exactly.

3. const arr = [1, 2, 3]; arr[100] = 4; console.log(arr.length);

Answer: 101
Explanation: JavaScript arrays are sparse, meaning that you can set an index that is greater than the current length of the array. The length property will then reflect the highest index + 1.

4. console.log(typeof null)

Answer: object
Explanation: null is considered an object due to a historical bug that has never been fixed duo to backward compatibility

5. function func(a, b = '', c, ...rest) {} console. log(func.length);

Answer: 1
Explanation: The length property of a function indicates the number of expected parameters before the first optional one, excluding rest parameters.

6. console.log(typeof NaN);

Answer: number
Explanation: NaN stands for 'Not a Number', but its type is actually 'number'.

7. console.log("10" - 5); console.log("10" + 5);

Answer: 5 105
Explanation: Subtraction converts '10' to a number. The second operation is concatenation, which converts 5 to a string.

8. console.log([10, 2, 5, 1].sort());

Answer: [1, 10, 2, 5]
Explanation: The sort method sorts the array elements as strings, so '10' comes before '2' and '5'.

9. for (var i = 0; i < 3; i++) { setTimeout(() => console.log(i), 1000); }

Answer: 3 3 3
Explanation: The variable 'i' is declared with var, which has function scope. By the time the setTimeout callback executes, 'i' is 3.

10. let a = 5; let b = a++ + ++a; console.log(b);

Answer: 12
Explanation: The value of a is 5. a++ returns 5 and then increments a to 6. ++a increments a to 7 and returns 7. So, b = 5 + 7 = 12.

11. console.log("ba" + +"a" + "a");

Answer: baNaNa
Explanation: The unary plus operator tries to convert the string 'a' to a number, which results in NaN. So, the expression becomes 'ba' + NaN + 'a', which results in 'baNaNa'.