1. javascript
  2. /basics
  3. /booleans

Introduction to Booleans in JavaScript

Introduction to Booleans in JavaScript

JavaScript, like many programming languages, has a data type called a boolean. Booleans are used to represent true or false values and are often used in control flow statements such as if and while loops. In this article, we'll take a look at the basics of booleans in JavaScript and how to use them in your code.

Creating Booleans

There are two ways to create a boolean in JavaScript: by using the true and false literals or by using the Boolean() constructor. The true and false literals are used to create boolean values, while the Boolean() constructor can be used to convert a value of any other data type into a boolean.

Here's an example of creating a boolean using the true and false literals:

let isTrue = true;
let isFalse = false;

And here's an example of creating a boolean using the Boolean() constructor:

let isTrue = Boolean(1); // true
let isFalse = Boolean(0); // false

Note that any value other than 0, null, undefined, NaN, or an empty string is considered true when using the Boolean() constructor. Like so:

let isTrue = Boolean("Hello World"); // true
let isFalse = Boolean(""); // false
let isTrue = Boolean([]); // true
let isTrue = Boolean({}); // true

Comparison

Booleans are often used in comparison statements to check if a value is true or false. Here are some examples of comparing booleans:

let isTrue = true;
let isFalse = false;

console.log(isTrue == true); // true
console.log(isTrue == false); // false
console.log(isFalse == false); // true

You can also use the ! operator to negate a boolean value:

let isTrue = true;
let isFalse = false;

console.log(!isTrue); // false
console.log(!isFalse); // true

Or use comparison, and logical operators to evaluate the condition:

// Comparison Operators:
// >	Greater than
// <	Less than
// >=	Greater than or equal to
// <=	Less than or equal to
// ==	Equal to
// !=	Not equal to
// ===	Equal value and equal type
// !==	Not equal value or not equal type

let num1 = 10;
let num2 = 20;

console.log(num1 > num2); // false
console.log(num1 < num2); // true
console.log(num1 >= num2); // false
console.log(num1 <= num2); // true
console.log(num1 == num2); // false
console.log(num1 != num2); // true
console.log(num1 === num2); // false
console.log(num1 !== num2); // true
// Logical Operators:
// &&	AND	(expr1 && expr2)
// ||	OR	(expr1 || expr2)

let num1 = 10;
let num2 = 20;

console.log(num1 > 5 && num2 < 25); // true
console.log(num1 > 5 || num2 < 15); // true

Using Booleans in Control Flow

Booleans are often used in control flow statements such as if and while loops to control the flow of execution in a program. Here's an example of using a boolean in an if statement:

let isTrue = true;

if (isTrue) {
    console.log("This code will run if the boolean is true");
} else {
    console.log("This code will run if the boolean is false");
}

And here's an example of using a boolean in a while loop:

let runLoop = true;
let count = 0;

while (runLoop) {
    console.log("The loop is running!");
    count++;
    if (count === 5) {
        runLoop = false;
    }
}

Conclusion

So far, we've covered the basics of booleans in JavaScript and how to use them in your code. We've looked at:

  • Creating booleans using the true and false literals and the Boolean() constructor

  • Comparing booleans using comparison, logical and ternary operators

  • Using booleans in control flow statements like if and while loops

Key takeaways:

  • Understanding the basics of booleans is essential for making decisions in your code

  • Comparison and logical operators can be used to evaluate conditions and make decisions based on those evaluations

  • The ternary operator can simplify the if-else statement