Advertisement
JavaScript & AJAX

Boolean()

by

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.

Related Posts
  • Code
    JavaScript & AJAX
    JavaScript ObjectsCover
    In JavaScript, objects are king: Almost everything is an object or acts like an object. Understand objects and you will understand JavaScript. So let's examine the creation of objects in JavaScript.Read More…
  • Code
    JavaScript & AJAX
    Function()Cover
    A function is a container of code statements that can be invoked using the parentheses () operator. Parameters can be passed inside of the parentheses during invocation so that the statements in the function can access certain values when the function is invoked.Read More…
  • Code
    JavaScript & AJAX
    Object()Cover
    Using the built-in Object() constructor function, we can create generic empty objects on the fly. In fact, if you remember back to the beginning of Chapter 1, this is exactly what we did by creating the cody object. Lets recreate the cody object.Read More…
  • Code
    JavaScript & AJAX
    String()Cover
    The String() constructor function is used to create string objects and string primitive values.Read More…
  • Code
    JavaScript & AJAX
    Function Prototype PropertyCover
    The prototype property is an object created by JavaScript for every Function() instance. Specifically, it links object instances created with the new keyword back to the constructor function that created them. This is done so that instances can share, or inherit, common methods and properties. Importantly, the sharing occurs during property lookup. Remember from the first article, that every time you look up or access a property on an object, the property will be searched for on the object as well as the prototype chain.Read More…
  • Code
    JavaScript & AJAX
    Working With Primitive String, Number and Boolean ValuesCover
    Do not be mystified by the fact that string, number, and Boolean literals can be treated like an object with properties (e.g., true.toString()). When these primitive values are treated like objects by attempting to access their properties, JavaScript will create a wrapper object from the primitive’s associated constructor, so that the properties and methods of the wrapper object can be accessed.Read More…