75

What is the use of:

var flag = new Boolean(false); 

compared to:

var flag = false;

When would you actually use new Boolean?

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
zeroin23
  • 1,821
  • 4
  • 20
  • 26
  • 1
    See also [What's the point of the Boolean Object?](http://stackoverflow.com/q/6132949/1048572) – Bergi Sep 17 '15 at 16:32

4 Answers4

77

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.

user123444555621
  • 148,182
  • 27
  • 114
  • 126
Christoph
  • 164,997
  • 36
  • 182
  • 240
  • 7
    Calling Boolean(someVar) can be useful to cast to a boolean primative, but remember that Boolean(false) == false, but new Boolean(false) == true, because it is an object – jackocnr Oct 07 '13 at 20:55
  • 1
    You don't always need to use `valueOf`--it will be called automatically when a primitive is needed. For instance, `boolean_object === true` will coerce the object to its underlying primitive (but `!boolean_object` won't). –  Oct 10 '14 at 10:02
  • 4
    @torazaburo: `===` doesn't coerce - `new Boolean(true) === true` is false – Christoph Oct 10 '14 at 16:14
  • 3
    @Christoph Sorry, my mistake. However, `boolean_object == true` (two equal signs, not three) **WILL** coerce (I'm pretty sure, just tested it). –  Oct 10 '14 at 16:16
  • @torazaburo: `==` does coerce - the algorithm is described in ECMA-262 ed5, section 11.9.3; translating `new Boolean(false) == false` back to Javascript, the actual comparison that is performed is `Number(new Boolean(false).valueOf()) === Number(false)`; the algorithm has some 'interesting' consequences, eg `new Number(0) == false` even though `Boolean(new Number(0)) === true` – Christoph Oct 10 '14 at 16:28
  • Modernizr does this to create booleans for features that have sub-features'. `bool = new Boolean(bool); bool.ogg = ...; bool.h264 = ...; bool.webm = ...;` – Simon_Weaver Jan 17 '15 at 02:05
  • "*boxing might be useful in cases where a reference to a primitive value is needed*" I don't see why a reference to a primitive value would be useful since primitive values are immutable. – Donald Duck Sep 01 '22 at 17:36
26

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.

user123444555621
  • 148,182
  • 27
  • 114
  • 126
  • 4
    You claim a general preference for a pair of *not* operators `!!`. But the purpose and effect of `!!` seems less obvious than the conventional `Boolean()` converter, and I wonder if code clarity is a concern here. – Michael Allan Sep 29 '17 at 15:04
  • 2
    One note. Don't ever use `new` which results in a `Boolean` **Object**. Then every time you'll have to use `foo.valueOf()` in conditionals. Instead just call `Boolean` as a function which simply results in type casting, i.e. `Boolean(bar) === !!bar` – Steven Pribilinskiy Apr 27 '18 at 08:30
  • if you need boolean to JSONify into something else than true or false then you may need a custom object – Pui Ho Lam Mar 22 '23 at 19:35
5

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

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

Next: everything with real value return true. E.g

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

everthing without real value return false E.g

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 value. The value passed as the first parameter is converted to a boolean value, if necessary. If value is omitted or is 0, -0, null, false, NaN, undefined, or the 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 very powerful tricks.

Derlin
  • 9,572
  • 2
  • 32
  • 53
-1

Interesting question:

You use new Boolean to create a boolean object. There can be many scenarios but I have discussed below one scenario.

Suppose you want a comparison in your code where you want to match string value and its datatype and it has to bool (true/false) then you will use new boolean instead of assigning 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");
}
jxpx777
  • 3,632
  • 4
  • 27
  • 43
Sohail Arif
  • 279
  • 2
  • 9