https://bigfrontend.dev/quiz/true-or-false
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)));
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
-
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 totrue
;!true
results infalse
, and finally!false
returnstrue
. -
Boolean(value)
returnsfalse
if the value passed as argument is falsy, otherwise it returnstrue
. An empty array is truthy, thusBoolean([])
returnstrue
. -
new Boolean(value)
returns aBoolean
object. Any object is truthy including aBoolean
object whose value isfalse
, thusBoolean(new Boolean(false))
returnstrue
.