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, read the following sentence 'If it is raining outside I will have to take an umbrella'. The condition here is: 'Is it raining?' and the action to be taken if it's met is: 'to take 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.
Creating Booleans
As with numbers and strings, to create a Boolean in JavaScript you could either take the literal way or the constructor way.
Using the literals
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.
Following is an illustration:
var rainyDay = true;
var batteryLow = false;
The typeof
operator as used with Boolean primitives, returns "boolean"
:
var rainyDay = true;
var batteryLow = false;
console.log(typeof rainyDay); // "boolean"
console.log(typeof batteryLow); // "boolean"
Create two Boolean variables submitted
and error
, and assign them 'falsy' values using Boolean literals.
The term 'falsy' simply means to give the variables the value false
. Following is the code that accomplishes the task above:
var submitted = false, error = false;
The Boolean()
constructor
The Boolean()
constructor is the other way to create a boolean in JavaScript. Specifically it creates a Boolean
object.
Following is an illustration:
var rainyDay = new Boolean(true);
var batteryLow = new Boolean(false);
Since this time we've used a constructor to create Boolean values, typeof
will return "object"
for the variables above.
console.log(typeof rainyDay); // "object"
console.log(typeof batteryLow); // "object"
true
or false
regardless. So, there's really no benefit of using the constructor - and we even shouldn't!Things to note
-
All comparison operators return Boolean primitives:
typeof (1 == 1); // true typeof (1 > 1); // false typeof (1 === "1"); // false
-
Logical operators, if used with Boolean values combine the result and again return a Boolean primitive:
typeof (true && true); // true typeof (true && false); // false typeof (true && false && true); // false typeof (true || false); // true
For more information on how logical operators work see Logical operators explanation.
Converting to Boolean
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.
Boolean()
converts its argument (values inside the parentheses) into a Boolean primitive value.
Boolean()
function, not the Boolean()
constructor.With numbers:
All numbers except for 0
get converted to the Boolean value true
. The number 0
gets converted to false
.
Boolean(10.5); // true
Boolean(0.001); // true
Boolean(-100); // true
Boolean(0); // false
Consider the following code:
var a = 200;
console.log(typeof a); // "number"
a = Boolean(a);
console.log(typeof a); // "boolean"
First, the variable a
is a number which can be confirmed from line 2, and is then converted into a Boolean in line 4. Consequently, in line 5, typeof a
returns "boolean"
.
With strings:
All strings, except for the empty string ""
(or ''
, ``
), get converted to true
. The empty string gets converted to false
.
Following is an illustration:
Boolean("Hello"); // true
Boolean("0"); // true
Boolean(""); // false
Boolean(''); // false
Boolean(``); // false
With objects:
All objects get converted to the value true
, even the ones without any properties.
var obj = {};
var b = Boolean(arr); // true
typeof b; // "boolean"
true
Boolean values.The following example shows a detailed view of what gets converted to true
and what to false
when passed to the Boolean()
function.
// 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
- All numbers except zero (like 0, 0.00, 0.000 and so on)
- All strings except the empty strings -
""
,''
, or``
. - The boolean
true
- All arrays and objects.This is the reason why the number created from
new Number(0)
, the string created fromnew String("")
and the Boolean created fromnew Boolean(false)
all returntrue
- 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.