W3cubDocs

/JavaScript

Boolean() constructor

The Boolean() constructor can create Boolean objects or return primitive values of type boolean.

Try it

Syntax

new Boolean(value)
Boolean(value)

Note: Boolean() can be called with or without new, but with different effects. See Return value.

Parameters

value

The initial value of the Boolean object.

Return value

When Boolean() is called as a constructor (with new), it creates a Boolean object, which is not a primitive.

When Boolean() is called as a function (without new), it coerces the parameter to a boolean primitive.

Warning: You should rarely find yourself using Boolean as a constructor.

Examples

Creating Boolean objects with an initial value of false

const bZero = new Boolean(0);
const bNull = new Boolean(null);
const bEmptyString = new Boolean('');
const bfalse = new Boolean(false);

typeof bfalse // "object"
Boolean(bfalse) // true

Note how converting a Boolean object to a primitive with Boolean() always yields true, even if the object holds a value of false. You are therefore always advised to avoid constructing Boolean wrapper objects.

If you need to take the primitive value out from the wrapper object, instead of using the Boolean() function, use the object's valueOf() method instead.

const bfalse = new Boolean(false);

bfalse.valueOf() // false

Creating Boolean objects with an initial value of true

const btrue = new Boolean(true);
const btrueString = new Boolean('true');
const bfalseString = new Boolean('false');
const bSuLin = new Boolean('Su Lin');
const bArrayProto = new Boolean([]);
const bObjProto = new Boolean({});

Specifications

Browser compatibility

Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet Deno Node.js
Boolean
1
12
1
3
4
1
4.4
18
4
10.1
1
1.0
1.0
0.10.0

See also

© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean/Boolean