JavaScript Booleans

Whta is JavaScript Booleans?

In JavaScript, Booleans represent one of two values: true or false. They are essential in programming because they drive decision-making and logic. Booleans are commonly used in conditional statements, loops and comparisons to control the flow of a program.

Boolean Values in JavaScript

A Boolean has only two possible values:

  • true (truthy)
  • false (falsy)

Example:

let isJavaScriptFun = true;
let isRainy = false;

console.log(isJavaScriptFun); // Output: true
console.log(isRainy); // Output: false

Boolean Conversion

JavaScript can automatically convert values to Booleans using truthy and falsy concepts.

Truthy Values

Values that are considered true in Boolean context:

  • Non-empty strings (“Hello”)
  • Non-zero numbers (42, -1)
  • Objects ({}) and Arrays ([ ])
  • The Boolean true itself

Falsy Values

Values that are considered false in Boolean context:

  • 0
  • null
  • undefined
  • NaN (Not-a-Number)
  • Empty string (“”)
  • The Boolean false

Example:

console.log(Boolean(0));         // Output: false
console.log(Boolean("Hello")); // Output: true
console.log(Boolean(null)); // Output: false
console.log(Boolean([])); // Output: true

Boolean Comparison Operators

Comparison operators return Boolean values (true or false) based on conditions.

1. Equality Operators

  • == (Loose equality): Compares values after type conversion.
  • === (Strict equality): Compares both value and type.

Example:

console.log(5 == "5");  // Output: true (loose equality)
console.log(5 === "5"); // Output: false (strict equality)

2. Inequality Operators

  • != (Loose inequality): Checks if values are not equal after type conversion.
  • !== (Strict inequality): Checks if values and types are not equal.

Example:

console.log(10 != "10");  // Output: false
console.log(10 !== "10"); // Output: true

3. Comparison Operators

  • < (less than)
  • > (greater than)
  • <= (less than or equal to)
  • >= (greater than or equal to)

Example:

console.log(10 > 5);   // Output: true
console.log(10 <= 5); // Output: false

Boolean Logical Operators

Logical operators are used to combine or modify Boolean expressions:

1. AND (&&)

Returns true if all conditions are true.

Example:

let age = 25;
let hasLicense = true;

console.log(age > 18 && hasLicense); // Output: true

2. OR (||)

Returns true if at least one condition is true.

Example:

let isWeekend = false;
let isHoliday = true;

console.log(isWeekend || isHoliday); // Output: true

3. NOT ( ! )

Reverses the Boolean value.

Example:

let isRaining = true;

console.log(!isRaining); // Output: false

Using Booleans in Conditional Statements

Booleans are often used in if…else conditions to determine the flow of a program.

Example:

let hasPassword = true;

if (hasPassword) {
console.log("Access granted!");
} else {
console.log("Access denied!");
}
// Output: Access granted!

Booleans in Functions

Functions can return Boolean values to indicate a condition or success/failure.

Example:

function isEven(number) {
return number % 2 === 0;
}

console.log(isEven(4)); // Output: true
console.log(isEven(7)); // Output: false

Practical Example: Login System

Booleans are critical for validating user input.

Example:

function login(username, password) {
const validUsername = "admin";
const validPassword = "1234";

if (username === validUsername && password === validPassword) {
return true;
} else {
return false;
}
}

console.log(login("admin", "1234")); // Output: true
console.log(login("user", "abcd")); // Output: false

Leave a Comment