Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why two examples' output are different about Javascript Boolean? [duplicate]

Tags:

javascript

eg1:

 var boo = new Boolean(false)
 document.write(boo.valueOf())//false

eg2:

 var boo1 = new Boolean(new Boolean(false))
 document.write(boo1.valueOf())//true

Why two examples' output are different?

By the way:

console.log((new Boolean( new Boolean(false))))//nothing
document.write(new Boolean( new Boolean(false)))//true

Why is there nothing in the console?

like image 876
Finit Avatar asked Feb 04 '26 07:02

Finit


1 Answers

Objects are truthy, and when you use new Boolean, you're calling the Boolean constructor, which returns an object. When new Boolean is called with a truthy value, it results in a object whose value is true. Thus, new Boolean(new Boolean(<anything>)) will result in a Boolean with a value of true.

But just don't do this - use literal booleans or Boolean(condition) instead.

like image 104
CertainPerformance Avatar answered Feb 05 '26 20:02

CertainPerformance