Booleans are a data type with only true and false values. They’re useful because they allow you to do something based on some conditions. Booleans are essential when you want to evaluate whether something should happen or not, like deciding if someone can access a certain feature in your app. Here is an example of setting the value true to a variable called isOldEnoughToDrive:

let isOldEnoughToDrive = true;
 
console.log(isOldEnoughToDrive); // true

You can use this variable inside a conditional like this:

let isOldEnoughToDrive = true;
 
if (isOldEnoughToDrive) {
 console.log("You're old enough to drive"); // You're old enough to drive
} else {
 console.log("Sorry, you are not old enough to drive");
}

A conditional helps you make decisions in your code based on a condition. This example uses what is called an if/else statement.

If isOldEnoughToDrive is true, then the sentence You're old enough to drive will be logged to the console. Otherwise, if the isOldEnoughToDrive is false, then the sentence Sorry, you are not old enough to drive will be logged to the console. Since the isOldEnoughToDrive variable is set to true, the first sentence will be logged to the console. You will learn more about if/else statements in a future lecture video.

To compare two values, you can use either the equality or strict equality operator. The result of the comparison will be a boolean of either true or false. Here is an example of using the equality operator to compare a string and a number. The equality operator is represented by a double equals sign (==).

console.log(5 == "5"); // true

In this example, JavaScript converts the string "5" into the number 5 and then checks if they are equal. Since both values are now the same, the result is true. The equality operator uses type coercion before checking if each value is equal.

This differs from the strict equality operator, which does not perform type coercion. The strict equality operator will check if the types are the same and if the values are the same. Here is an example using the strict equality operator to compare a number and string. This operator is represented by a triple equals sign (===).

console.log(5 === '5'); // false

The following comparison will be false, because a string data type is not the same as a number data type. If you need to check if something is not equal to another value, then you can use the inequality or strict inequality operators. Here is an example of using the inequality operator (!=) to compare a number with a string.

console.log(5 != "5"); // false

In this example, the result would be false because the inequality operator first converts the string value to a number and then compares the values. Since the values would be the same it will return false. If you tried to use the strict inequality operator, then you would get a different result. The strict inequality operator is represented by an exclamation mark followed by two equal signs (!==).

console.log(5 !== "5"); // true

The result would be true because the strict inequality operator does not perform any type coercion. Since the number 5 is not equal to the string "5", then the result is true.

It is considered best practice to use strict inequality and equality operators whenever possible, as they do not perform type coercion.