Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What is the use of:

var flag = new Boolean(false);  

compared to:

var flag = false; 

When would you actually use new Boolean?

like image 716
zeroin23 Avatar asked May 13 '09 06:05

zeroin23


People also ask

What is the purpose of Boolean JavaScript?

In JavaScript, Boolean is used as a function to get the value of a variable, object, conditions, expressions, etc. in terms of true or false.

What does New Boolean return?

new Boolean(false) returns an object. All objects (except document. all in browsers) are truthy. As a result, ! of any object will always be false .

What is Boolean in JavaScript with example?

In JavaScript, booleans are the primitive data types that can either be true or false . For example, const a = true; const b = false; Note: If you wrap true or false in a quote, then they are considered as a string.

What is the purpose of a boolean variable?

Boolean variables can either be True or False and are stored as 16-bit (2-byte) values. Boolean variables are displayed as either True or False. Like C, when other numeric data types are converted to Boolean values then a 0 becomes False and any other values become True.


2 Answers

The global function Boolean() can be used for type casting when called without new, eg

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 as primitives can't hold properties:

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

Assigning a property to a primitive doesn't produce an error because of auto-boxing, ie

foo.baz = 'quux'; 

will be interpreted as

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

To get the primitive value back, you'll have to invoke the valueOf() method. This is needed if you want to actually use the wrapped value, because objects always evaluate to true in boolean contexts - even if the wrapped value is false.

I've never come across a useful application of being able to assign properties to booleans, but boxing might be useful in cases where a reference to a primitive value is needed.

like image 172
Christoph Avatar answered Oct 19 '22 16:10

Christoph


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

Because Boolean objects (as objects in general) are always truthy, it is considered bad practice to use them. In many years of JS programming, I have never used them, and I can't remember seeing Booleans in other peoples' code either. Not even once.

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

If you ever need a bool wrapped in an object, you might as well use an Object object like so:

foo = { value: false }; 

Also, calling the Boolean() constructor as a function (as in foo = Boolean(bar)) has the same effect as explicit typecasting using !!, and the latter is generally preferred over the former.

like image 27
user123444555621 Avatar answered Oct 19 '22 16:10

user123444555621