Skip to content

Latest commit

 

History

History
55 lines (39 loc) · 1.88 KB

26.true-or-false.md

File metadata and controls

55 lines (39 loc) · 1.88 KB

26. true or false

Problem

https://bigfrontend.dev/quiz/true-or-false

Problem Description

What does the code snippet below output by console.log?

console.log([] == 0);
console.log([] == false);
console.log(!![]);
console.log([1] == 1);
console.log(!![1]);
console.log(Boolean([]));
console.log(Boolean(new Boolean([])));
console.log(Boolean(new Boolean(false)));

Answer

console.log([] == 0); // true
console.log([] == false); // true
console.log(!![]); // true;
console.log([1] == 1); // true;
console.log(!![1]); // true;
console.log(Boolean([])); // true
console.log(Boolean(new Boolean([]))); // true
console.log(Boolean(new Boolean(false))); // true

Explanation

  • The double NOT operator explicitly converts any value to the corresponding boolean primitive. The conversion is based on the "truthyness" or "falsyness" of the value.
    In the case of !![], since any array is truthy, [] evaluates to true; !true results in false, and finally !false returns true.

  • Boolean(value) returns false if the value passed as argument is falsy, otherwise it returns true. An empty array is truthy, thus Boolean([]) returns true.

  • new Boolean(value) returns a Boolean object. Any object is truthy including a Boolean object whose value is false, thus Boolean(new Boolean(false)) returns true.

Reference