The boolean logic is named after George Boole, who is the pioneer in the field of mathematical logic. At the very core of boolean logic is the idea that all values can be either true or false, and this has been translated into computer science as booleans – a logical data type that can only have two values, either true or false, on or off, yes or no, etc. In this article, we’re going to look into booleans basics, we will get to know the concept of falsy and truthy values, and at the end, we will get to know what boolean() object is all about.
A primitive JavaScript Boolean: true or false
When programming, you will often need a data type that can have only two values, such as on/off, yes/no, and as already noticed – true or false. These data types are called Boolean data types. JavaScript booleans are a primitive type of data, which means you don’t have to create them as an object explicitly. The reserved words – true or false – are used to assign a boolean value to a variable, and we follow the same logic when we create a boolean in JSON.
In JavaScript, we use boolean as a function to get the boolean value – either true or false – of a variable, object, conditions, expression.
Let’s take a look at a simple example where we assign a boolean value as true and false to the following variables:
let example1 = true;
let example2 = false;
Do not specify a boolean value as a string
Keep in mind that strings are not the correct data type for boolean expressions:
let example1 = "true"; // not a boolean
Do not specify a boolean value as an object
When you create booleans as objects, it may result in an unnecessary complication of a code.
let example2 = my_Boolean(true); // overcomplicated
Some JavaScript control statements rely on booleans to evaluate conditions
Some JavaScript control statements only execute under specific conditions, and in cases like these, we count on booleans. This evaluation mechanism is crucial, for example in while loops and if-else statements.
Below, we will show an example of a while loop, where we will assume that myBoolean() is a Javascript function that will return a boolean value:
let example1 = myBoolean();
while (example1 === true) {
... some code...
}
And here, we show an example of if-else statement:
let example1 = myBoolean();
if (example1 === true) {
... code if true ...
} else {
... code if false ...
}
Remember, the Boolean value (true vs. false) of an expression is the basis for all JavaScript comparisons and conditions.
What should you know about truthy and falsy values?
We’ve already explained that in JavaScript booleans result in either true or false values. But what are truthy and what are falsy values?
Usually, a truthy value comes from an object with a defined value or a structure, and this can be:
- an empty array:
let example = [];
- an empty object:
let example = {};
- a majority of numbers, even negative numbers:
let example = -5;
- strings that are not empty, for example:
let example = "my string";
You should take for granted that all values are truthy unless they are defined as falsy, and even when values are defined as falsy, they can sometimes differ between different browsers and different Javascript implementations.
On the other hand, a falsy or falsey value comes from an object that does not have a defined value or a structure, and this can be:
- null values:
let example = -0;
- undefined values, such as values for Not A Number:
let example = NaN;
- strings that are empty, for example:
let example = "";
What about JavaScript booleans defined as objects?
As we presented above, JavaScript booleans are primitive values created from literals, but booleans can be also defined as objects. In cases like these, when we want to define them as objects, we use a keyword new. Let’s take a look at an example below:
let example1 = false; // typeof example1 returns boolean
let example2 = new Boolean(false); // typeof example2 returns object
In addition to that, any object whose value is not undefined or null, including a Boolean object whose value is false, evaluates to true when passed to a conditional statement. In the example below, the condition in the following if statement evaluates to true:
let example = new Boolean(false);
if (example) {
// this code is executed
}
However, as a rule of thumb, it is advised not to create Boolean objects, because they can produce unexpected results, and the “new” keyword complicates the code and slows down execution speed.
In the context of booleans, we should also bring out implicit and explicit coercion. The explicit coercion occurs when types are converted directly using syntax like Boolean(value). Implicit coercion is however much more interesting and is recognized when specific operators and expressions are used. The loose (==) and strict (===) equality operators are acknowledged for this behavior.
You should pay attention to whether you’re using equal-to or loose ( == ) or strict operator ( === ). Use only strict operators to evaluate boolean expressions because truthy and falsy expressions only evaluate as equal to explicit booleans if you are not using strict comparisons. In general, if you’re comparing two JavaScript objects, JavaScript will always return false.
Let’s explore an example where we bring out loose and strict operators.
The first example returns equal because we used a nonstrict operator ( == ), so the example is evaluated as true because a non-empty string is truthy:
let example = ("10" == true);
In the second example, we used a strict comparison operator ( === ), and our string is considered false because of that:
let example = ("10" === true);
So, what is the takeaway message?
Booleans are one of the main data types and in JavaScript, many concepts are dependant on them. You should learn to distinguish between explicit booleans and implicit coercion – a proper distinction will help you avoid common programming mistakes.