The Boolean()
constructor function can be used to create Boolean objects, as well as Boolean primitive values, that represent either a true
or a false
value.
In the following code, I detail the creation of Boolean values in JavaScript.
Sample: sample52.html
<!DOCTYPE html><html lang="en"><body><script> // Create a Boolean object using the new keyword and the Boolean() constructor. var myBoolean1 = new Boolean(false); // Using new keyword. console.log(typeof myBoolean1); // Logs 'object'. // Create a Boolean literal/primitive by directly using the number constructor without new. var myBoolean2 = Boolean(0); // Without new keyword. console.log(typeof myBoolean2); // Logs 'boolean'. // Create Boolean literal/primitive (constructor leveraged behind the scenes). var myBoolean3 = false; console.log(typeof myBoolean3); // Logs 'boolean'. console.log(myBoolean1, myBoolean2, myBoolean3); // Logs false false false. </script></body></html>
Boolean()
Parameters
The Boolean()
constructor function takes one parameter to be converted to a Boolean value (i.e. true
or false
). Any valid JavaScript value that is not 0, -0, null
, false
, NaN
, undefined
, or an empty string ("") will be converted to true
. In the following sample, we create two Boolean object values: One true
and one false
.
Sample: sample53.html
<!DOCTYPE html><html lang="en"><body><script> // Parameter passed to Boolean() = 0 = false, thus foo = false var foo = new Boolean(0) console.log(foo); // Parameter passed to Boolean() = Math = true, thus bar = true var bar = new Boolean(Math) console.log(bar); </script></body></html>
When used with the new
keyword, instances from the Boolean()
constructor produce an actual complex object. You should avoid creating Boolean values using the Boolean() constructor (instead, use literal/primitive numbers) due to the potential problems associated with the typeof
operator. The typeof
operator reports Boolean objects as 'object', instead of the primitive label ('boolean') you might expect. Additionally, the literal/primitive value is faster to write.
Boolean()
Properties and Methods
The Boolean()
object has the following properties:
Properties (e.g., Boolean.prototype;
):
Boolean Object Instance Properties and Methods
Boolean object instances have the following properties and methods (not including inherited properties and methods):
Instance Properties (e.g., var myBoolean = false;
myBoolean.constructor;
):
Instance Methods (e.g., var myNumber = false;
myBoolean.toString();
):
Non-Primitive False Boolean Objects Convert to True
A false
Boolean object (as opposed to a primitive value) created from the Boolean()
constructor is an object, and objects convert to true
. Thus, when creating a false
Boolean object via the Boolean()
constructor, the value itself converts to true
. In the following sample, I demonstrate how a false
Boolean object is always "truthy.”
Sample: sample54.html
<!DOCTYPE html><html lang="en"><body><script> var falseValue = new Boolean(false); console.log(falseValue); // We have a false Boolean object, but objects are truthy. if (falseValue) { // Boolean objects, even false Boolean objects, are truthy. console.log('falseValue is truthy'); } </script></body></html>
If you need to convert a non-Boolean value into a Boolean, just use the Boolean()
constructor without the new
keyword and the value returned will be a primitive value instead of a Boolean object.
Certain Things Are False, Everything Else Is True
It has already been mentioned, but is worth mentioning again because it pertains to conversions: If a value is 0, -0, null
, false
, NaN
, undefined
, or an empty string(""), it is false
. Any value in JavaScript except the aforementioned values will be converted to true
if used in a Boolean context (i.e. if (true) {};
).
Sample: sample55.html
<!DOCTYPE html><html lang="en"><body><script> // All of these return a false Boolean value. console.log(Boolean(0)); console.log(Boolean(-0)); console.log(Boolean(null)); console.log(Boolean(false)); console.log(Boolean('')); console.log(Boolean(undefined)); console.log(Boolean(null)); // All of these return a true Boolean value. console.log(Boolean(1789)); console.log(Boolean('false')); // 'false' as a string is not false the Boolean value. console.log(Boolean(Math)); console.log(Boolean(Array())); </script></body></html>
Conclusion
It's critical that you understand which JavaScript values are reduced to false
so you are aware that all other values are considered true
.
Comments