JavaScript Guess the Output #2 - Advanced Concepts

Select one option

1 / 10

console.log(NaN === NaN);

TL;DR answer sheet

JavaScript Guess the Output #2 - Advanced Concepts — quiz answer sheet, click to expand.

1. console.log(NaN === NaN);

Answer: false

Explanation: NaN is the only value in JavaScript that is not equal to itself. This is a characteristic feature of NaN, to check for it use `isNaN()`.

2. console.log([] == ![]);

Answer: true

Explanation: The `!` operator converts its operand to a boolean and then negates it. `[]` is truthy, so `![]` is `false`. The `==` operator then attempts type coercion. `[]` is converted to an empty string `''`, and `false` is converted to `0`. `'' == 0` is `true`.

3. let x = 10; function foo() { console.log(x); let x = 20; } foo();

Answer: ReferenceError

Explanation: This is a temporal dead zone (TDZ) error. `x` inside `foo` is block-scoped due to `let`. When `console.log(x)` is executed, `x` has been declared but not yet initialized, leading to a `ReferenceError`.

4. console.log(typeof null === 'object' && null === null);

Answer: true

Explanation: The `typeof null` returns 'object' (a long-standing bug in JavaScript). `null === null` is true. Both conditions are true, so the result is true.

5. const obj = { a: 1 }; const arr = [obj]; obj.a = 2; console.log(arr[0].a);

Answer: 2

Explanation: Objects are assigned by reference. `arr[0]` holds a reference to `obj`. When `obj.a` is modified, the change is reflected in all references to that object, including `arr[0]`.

6. console.log(parseInt('10.5 degrees'));

Answer: 10

Explanation: `parseInt()` parses a string argument and returns an integer. It stops parsing at the first character it encounters that is not a digit or sign, so '10.5 degrees' becomes 10.

7. console.log([,,].length);

Answer: 2

Explanation: When using array literal syntax with commas, each comma implicitly creates an empty slot. `[,,]` has two empty slots (indices 0 and 1), hence a length of 2.

8. console.log(true + false);

Answer: 1

Explanation: When using the `+` operator with booleans, `true` is coerced to `1` and `false` to `0`. So, `1 + 0` equals `1`.

9. console.log({} + []);

Answer: 0

Explanation: When `{}` appears at the beginning of a statement, it's often parsed as an empty code block, not an object literal. The expression then becomes `+[]`. The unary plus operator converts the empty array `[]` to a number, which is `0`.

10. console.log([] + {});

Answer: [object Object]

Explanation: When the `+` operator is used with an array and an object, JavaScript attempts to convert them to primitive values. `[]` converts to an empty string `''`. `{}` converts to the string `'[object Object]'`. The `+` operator then performs string concatenation.

Similar Quizzes