Edit Template
Edit Template

JavaScript Boolean

In JavaScript, a Boolean represents one of two values: true or false. These values are essential in controlling the flow of programs using conditions, comparisons, and logical operations.

Creating Boolean Values

You can create Boolean values in two main ways:

1. Using Boolean literals:
				
					let isAvailable = true;
let isClosed = false;

				
			
2. Using the Boolean() constructor to convert values:
				
					console.log(Boolean(5));        // true (non-zero number)
console.log(Boolean("text"));   // true (non-empty string)
console.log(Boolean(0));        // false (zero is false)
console.log(Boolean(""));       // false (empty string)


				
			

Truthy vs Falsy

JavaScript treats some values as false when evaluated in a Boolean context. These are known as falsy values:

  • false

  • 0

  • "" (empty string)

  • null

  • undefined

  • NaN

All other values are considered truthy.

Logical Operators

Logical operators help make decisions in JavaScript:

&& (Logical AND)

Returns true only if both expressions are true.

				
					let loggedIn = true;
let hasAccess = true;
console.log(loggedIn && hasAccess);  // true

				
			
|| (Logical OR)

Returns true if at least one expression is true.

				
					let admin = false;
let editor = true;
console.log(admin || editor);  // true

				
			
! (Logical NOT)

Inverts the value.

				
					let isVerified = false;
console.log(!isVerified);  // true

				
			

Boolean as an Object

While it’s rare in practice, you can create a Boolean object using new Boolean():

				
					let b = new Boolean(false);
console.log(b);             // Boolean {false}
console.log(b.valueOf());   // false

				
			

Note: Boolean objects behave differently than primitive booleans in conditional checks, so use them carefully.

Practical Use Case

Let’s check if a user is eligible based on age and login status:

				
					let isLoggedIn = true;
let age = 18;

let eligible = isLoggedIn && age >= 18;
console.log("Eligible for dashboard:", eligible);  // true


				
			

Conclusion

ConceptExampleResult
Boolean(0)Boolean(0)false
Boolean("hello")Boolean("hello")true
ANDtrue && falsefalse
OR`false 
NOT!truefalse
Scroll to Top