What are Booleans?

In computer programming, Booleans are true or false values that sit at the heart of conditional programming. The name is in the honour to the mathematician George Boolean - you should consider searching about him for a while!

There are thousands of instances in programming where we need to perform certain actions once certain conditions are met.

As an analogy, 'If it is raining outside I will have to take an umbrella'. The condition is: 'Is it raining?' and the action to be taken if it is met is: 'to take out an umbrella'. In computer terminology we can take the example 'If the battery is low, automatically switch off the computer'. Again you have a condition and an action. And there can be many such examples that operate on the same idea.

In computers, we use boolean values to check whether some condition is met or not. All programming languages follow the same principle of booleans and thus make conditional programming fairly equivalent.

Boolean primitives and objects

To create a Boolean in JavaScript you could either use the literal way or the constructor way

The Boolean literals are true and false, without any quotation marks. They create a boolean primitive, just like we saw string literals create string primitives in the Strings Basic Concepts chapter.

typeof used with Boolean primitives will return "boolean".

var rainyDay = true;
var cloudyDay = false;

console.log(typeof rainyDay); // returns "boolean"
console.log(typeof cloudyDay); // returns "boolean"

The Boolean() constructor is the other way to create a boolean in JavaScript. Specifically it creates a boolean object.

var rainyDay = new Boolean(true);
var cloudyDay = new Boolean(false);

console.log(typeof rainyDay); // returns "object"
console.log(typeof cloudyDay); // returns "object"
Notice here that although literals are used inside the constructor, an object is returned.

Now just like in numbers and strings, it is better to use stay with the primitive form for simplicity.

Things to note

  1. All comparison operators return boolean primitives:
    console.log(typeof (1 == 1)); // returns true
    console.log(typeof (1 > 1)); // returns false
    console.log(typeof (1 === "1")); // returns false
  2. Logical operators if used with boolean values combine the result and again return a boolean primitive:
    console.log(typeof (true && true)); // returns true
    console.log(typeof (true && false)); // returns false
    console.log(typeof (true && false && true)); // returns false
    console.log(typeof (true || false)); // returns true
    For more information on how logical operators work see Logical operators explanation.

Boolean conversions

There are certain times where we might want to go from a given data type to a boolean. The Boolean() function helps us to do just this very thing.

It converts its arguments (values inside the parentheses) to boolean values.

With numbers:

var num1 = 10.58;
console.log(typeof num1); // returns "number"

// Conversion to boolean data type
console.log(typeof Boolean(num1)); // returns "boolean"

With strings:

var str1 = "I am a string";
console.log(typeof str1); // returns "string"

// Conversion to boolean data type
console.log(typeof Boolean(str1)); // returns "boolean"

With arrays and objects:

var arr = [];
console.log(typeof arr); // returns "object"

// Conversion to boolean data type
console.log(typeof Boolean(str1)); // returns "boolean"
Converting arrays and objects to booleans isn't very useful because even empty arrays and objects convert to true boolean values as you will see below.

The following example shows what gets converted to true and what to false when used within Boolean().

// Booleans
Boolean(true) // true
Boolean(false) // false
Boolean(new Boolean(false)) // true

// Numbers
Boolean(10.58) // true
Boolean(0) // false
Boolean(new Number(0)) // true

// Strings
Boolean("true") // true
Boolean("false") // true
Boolean(" ") // true
Boolean("") // false
Boolean('') // false
Boolean(new String("")) // true

// Arrays and Objects
Boolean(["name", "age", "gender"]) // true
Boolean([]) // true
Boolean(new Array()) // true
Boolean({name: "Something"}) // true
Boolean({}) // true
Boolean(new Object()) // true

Things that get converted to true

  1. All numbers except zero (like 0, 0.00, 0.000 and so on)
  2. All strings except non-empty ones (like "") convert to true
  3. The boolean true
  4. All arrays and objects.
    This is the reason why the number 0 created from new Number(), the string "" created from new String() and the boolean false created from new Boolean() all return true - because they return objects.

In conclusion

This chapter is very important in this unit because it establishes the fundamentals of booleans that are the fundamental concept of conditional programming. Therefore we suggest spending time experimenting with booleans in the developer's console.

In the next chapters we will move right into the use of these true and false values in real programs.