Specifies the function that created the object's prototype.
Allows you to define properties on the Boolean that is shared by all Boolean objects.
Returns a string specifying the value of the Boolean, in this case, "true" or "false."
Returns the primitive value of a Boolean object.
Booleanobject 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 isDo not confuse the primitive Boolean values true and false with the true and false values of the Boolean object.
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.
Any object whose value is not
null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. For example, the condition in the following if statement evaluates to true:
This behavior does not apply to Boolean primitives. For example, the condition in the following if statement evaluates to
Do not use a
Booleanobject to convert a non-boolean value to a boolean value. Instead, use Boolean as a function to perform this task:
If you specify any object, including a Boolean object whose value is false, as the initial value of a Boolean object, the new Boolean object has a value of true.
Do not use a Boolean object in place of a Boolean primitive.
Booleanobjects with an initial value of false
Booleanobjects with an initial value of true
Take booleans, for example. In most code, the primitive values are used, like so:
There is also the
Booleanfunction, which can be used as an ordinary function which returns a boolean primitive:
Booleanfunction can also be used as a constructor with the
The tricky thing here is that when
Booleanis used as a constructor, it doesn't return a primitive. Instead it returns an object.
It turns out that using the
Likewise, if you try to use an object in a context that expects a boolean value, the object will be coerced to
And since the
Booleanobject is an object, it will also coerce to
true, even if its internal value is
If you actually need to get at the internal value of a
Booleanobject, then you'll need to use the
But because of the quirks surrounding
Booleanobjects, you're probably best off avoiding them altogether. In fact, linting tools like JSHint and JSLint will flag the
Booleanconstructor as a potential error in your code.
In the event that you need to explicitly coerce another type of value into
false, you're better off using
Booleanas an ordinary function, or using the not operator twice.
The double not above is pretty simple, though it can be confusing if you haven't seen it before. Using a single not operator coerces the value into a boolean primitive and then reverses it. (To
truein this case). The second use of the not operator reverses the value again, so that it is flipped back to the correct boolean representation of the original value.