Booleans

Booleans are one of the most basic and important data types in JavaScript. They represent logical values and are used to control decision making in programs.

Introduction

A Boolean can have only two values: true or false. These values help determine the flow of a program based on conditions. Booleans are commonly used in comparisons, conditions, and loops.

Core Purpose of Booleans

Booleans help developers make decisions in code
They control the execution of conditions
They are used in comparisons and logical operations
They help manage application logic efficiently

Boolean Values

true means yes, correct, or enabled
false means no, incorrect, or disabled

Using Booleans in JavaScript

Booleans are often created through comparisons

let isLoggedIn = true;
let isAdmin = false;

let result = 10 > 5; // true
let check = 7 === 3; // false

Comparison Operators

Equal to checks if values are the same
Strict equal checks value and type
Not equal checks if values are different
Greater than and less than compare numbers

console.log(5 > 3);   // true
console.log(5 < 3); // false
console.log(5 == "5"); // true
console.log(5 === "5"); // false

Logical Operators

Logical operators work with Boolean values

AND returns true if both conditions are true
OR returns true if at least one condition is true
NOT reverses the value

let a = true;
let b = false;

console.log(a && b); // false
console.log(a || b); // true
console.log(!a); // false

Booleans in Conditions

Booleans are widely used in if statements

let age = 18;

if (age >= 18) {
console.log("You can vote");
} else {
console.log("You cannot vote");
}

Common Use Cases

Form validation to check user input
User authentication systems
Feature toggles in applications
Loop control and condition handling

Conclusion

Booleans are essential for controlling logic in JavaScript. Understanding how to use true and false values helps you write smarter and more efficient code.

Home ยป JavaScript Fundamentals (Beginner Level) > Variables and Data Types > Booleans