What is the purpose of the new Boolean () in Javascript?

What is the use of:

var flag = new Boolean(false); 

compared with:

 var flag = false; 

When do you actually use new Boolean ?

+51
javascript boolean
May 13 '09 at 6:10
source share
5 answers

The global function Boolean() can be used to cast when called without new , for example

 var foo = Boolean(bar); // equivalent to `var foo = !!bar` 

When called with new a wrapper object will be created additionally, which means that you can assign arbitrary properties to the object:

 var foo = new Boolean(bar); // equivalent to `var foo = Object(Boolean(bar));` foo.baz = 'quux'; alert(foo.baz); 

This is not possible with primitive values, since primitives cannot save properties:

 var foo = true; foo.baz = 'quux'; alert(foo.baz); // `foo.baz` is `undefined` 

Assigning a property to a primitive does not lead to an error due to automatic boxing, i.e.

 foo.baz = 'quux'; 

will be interpreted as

 // create and immediately discard a wrapper object: (new Boolean(foo)).baz = 'quux'; 

To return a primitive value, you have to call the valueOf() method. This is necessary if you want to actually use the wrapped value, because objects always evaluate true in Boolean contexts - even if the wrapped value is false .

I have never come across a useful application that allows you to assign properties to logical values, but boxing can be useful in cases where you need a reference to a primitive value.

+60
May 13 '09 at 9:35 a.m.
source share

While others mentioned the theory, let me talk about the practical part:

Since Boolean objects (like objects in general) are always true, consider them bad. Over the years of JS programming, I never used them, and I don’t remember to see Boolean in other peoples code. Not even once.

Using primitive values ​​will avoid confusion and make your code a little shorter.

If you need a bool wrapped in an object, you can also use an Object , for example:

 foo = { value: false }; 

In addition, calling the Boolean() constructor as a function (as in foo = Boolean(bar) ) has the same effect as casting explicitly when used !! , and the latter is usually preferable to the former.

+19
Jun 21 '13 at 6:29
source share

Boolean breed class. Instead of this spaghetti code 🍝:

 if (foo===true) this.launch(); else this.dontLaunch(); 

You can do what any great programmer will do and extend the prototype!

 Boolean.prototype.ifTrue=function(ifFunc,elseFunc){ if (this.valueOf()===true) ifFunc(); else elseFunc(); } var foo=new Boolean(/*expression*/); foo.ifTrue(this.launch.bind(this),this.dontLaunch.bind(this)); 

Much better.

+5
Sep 10 '14 at 14:52
source share

Before the above question, first Boolean functions, Boolean ()

 Boolean(10 > 4) // return true Boolean(4 > 9) // return false 

Next: everyone with a real value returns true. for example

 100 -4 4.4 "hello" "false" // note even the string value false return true. 

everthing with no real value return false Eg

 NaN var x = 10 / "H"; // Boolean(x); return false. undefined "" 0 -0 false null 

Now the Boolean object is an object wrapper for a boolean. The value passed as the first parameter is converted to a Boolean value if necessary. If the value is omitted or it is 0, -0, null, false, NaN, undefined or an empty string ( "" ), the object has an initial value of false. All other values, including any object or the string "false", create an object with an initial value of true.

This allows for very powerful tricks.

0
Apr 14 '16 at 1:18
source share

Interest Ask:

You are using the new Boolean to create a boolean. There may be many scenarios, but I discussed one scenario below.

Suppose you want to compare in your code where you want to match a string value and its data type, and it needs bool (true / false), then you will use the new boolean instead of assigning a simple false value.

 var flag = false; var flag2 = new Boolean (false); alert(typeof flag); //boolean object alert(typeof flag2); //simple object if (flag === flag2){ alert("Value and datatype match"); } else{ alert("Value and datatype do not match"); } 
-one
Jun 23 '15 at 20:59
source share



All Articles