2020年1月18日土曜日

Javascript Boolean()

Boolean()
Creates Boolean objects.
The Boolean object is an object wrapper for a boolean value. (ref)


what do you think the output of the following code ?
const bool = "false";
console.log(Boolean(bool)); 

it will print true.

because Boolean() evaluate values which are nil, or undefined to false but other value for example String to true.

const bool = "false";
console.log(bool == false); // false

console.log(Boolean(bool)); // true

also an empty array [] would be evaluated to true

const bool = [];
console.log(Boolean(bool)); // true


Don't confuse Boolean() and boolean primitive like true or false

0 件のコメント:

コメントを投稿