JavaScript

JavaScript is a high-level, interpreted programming language that is widely used for web development. Initially designed as a client-side scripting language, it runs directly in web browsers, enabling dynamic and interactive user experiences. JavaScript can now be used for server-side development as well.

JavaScript Booleans

8 April 2025 | Category:

In JavaScript, a Boolean represents one of two values:
➡️ true or false

Booleans are often used to control the flow of programs using conditions, comparisons, and loops.


🔹 Boolean Values

A Boolean value is either:

true
false

Example:

let isOnline = true;
let hasAccess = false;

🔍 Booleans from Comparisons

Booleans are commonly returned from comparison operations:

console.log(10 > 5);     // true
console.log(10 < 5);     // false
console.log(5 === 5);    // true
console.log("a" === "b"); // false

🔸 The Boolean() Function

You can convert other types to Boolean using:

Boolean(value);

Example:

Boolean(100);     // true
Boolean(0);       // false
Boolean("hello"); // true
Boolean("");      // false
Boolean(null);    // false
Boolean(undefined); // false

📋 Truthy and Falsy Values

In JavaScript, some values are truthy (evaluate to true) and others are falsy (evaluate to false).

✅ Truthy Values:

  • Non-zero numbers (1, -5, etc.)
  • Non-empty strings ("hello")
  • Objects, arrays, functions

❌ Falsy Values:

  • false
  • 0
  • "" (empty string)
  • null
  • undefined
  • NaN

🔁 Using Booleans in Conditions

let isLoggedIn = true;

if (isLoggedIn) {
  console.log("Welcome back!");
} else {
  console.log("Please log in.");
}

⚠️ Boolean vs String

Remember:
"false" is a string, not a Boolean!
It is truthy because it’s a non-empty string.

Boolean("false"); // true

📌 Summary

  • Booleans are true or false.
  • Used in comparisons, conditions, and logic.
  • Use Boolean() to convert values.
  • Know the difference between truthy and falsy.