Table of contents
1.
Introduction
2.
The Boolean() Function
2.1.
Syntax:
2.2.
Example
3.
JavaScript Boolean Properties  
3.1.
1. Boolean Values: `true` & `false`  
3.2.
2. Truthy & Falsy Values  
3.3.
3. Boolean Object  
3.4.
4. Boolean Conversion  
4.
Everything With a “Value” is True
4.1.
Example
5.
Everything Without a “Value” is False
5.1.
Example
6.
Boolean Coercion
6.1.
Example
7.
JavaScript Booleans as Objects
7.1.
Example
8.
Constructor and Instance
8.1.
Example
9.
Truthy and Falsy Values
9.1.
Truthy Values
9.2.
Falsy Values
9.3.
Example
10.
JavaScript Boolean Methods  
10.1.
1. `toString()` Method  
10.2.
2. `valueOf()` Method  
10.3.
3. Using Booleans in Conditional Statements  
10.4.
4. Logical Operators with Booleans  
10.5.
5. Boolean in Functions  
11.
Boolean Coercion in Conditions
11.1.
Example
12.
Practical Applications
12.1.
Example: Using Booleans in a Function
13.
Frequently Asked Questions
13.1.
What are truthy and falsy values in JavaScript?
13.2.
What is the Boolean() function in JavaScript?
13.3.
How are Booleans used in JavaScript?
14.
Conclusion
Last Updated: Jan 26, 2025
Easy

JavaScript Boolean

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

In JavaScript, the Boolean data type is used to represent two values: true and false. It is a fundamental concept for decision-making and controlling the flow of code. 

In this article, we will learn about JavaScript Boolean, its properties, methods, and how to use it for handling true or false values in programming.

The Boolean() Function

JavaScript provides the Boolean() function, which is used to convert other data types into a Boolean value (true or false). This function is helpful when you need to check whether a value is "truthy" or "falsy." Let's take a look at how it works:

Syntax:

Boolean(value);

Example

let num = 5;
console.log(Boolean(num)); 


let emptyString = "";
console.log(Boolean(emptyString));  
You can also try this code with Online Javascript Compiler
Run Code


Output: 

true
false


Explanation:

  • When you pass a number (5 in this case), the Boolean() function returns true because numbers other than 0 are truthy.
     
  • An empty string "" is falsy, so the result is false.

JavaScript Boolean Properties  

JavaScript Boolean has two primary properties: `true` & `false`. These are the only values a Boolean can hold. However, there are some built-in properties & behaviors associated with Booleans that make them versatile in programming. Let’s understand these in detail.  

1. Boolean Values: `true` & `false`  

A Boolean can only be `true` or `false`. These values are used to represent the truthiness or falsiness of a condition. For example:  

let isStudent = true;  
let isLoggedIn = false;  
console.log(isStudent); 
console.log(isLoggedIn); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

true
false  
You can also try this code with Online Javascript Compiler
Run Code


Here, `isStudent` is `true` because the condition (being a student) is true. On the other hand, `isLoggedIn` is `false` because the user is not logged in.  

2. Truthy & Falsy Values  

In JavaScript, values other than `true` or `false` can also behave like Booleans in certain contexts. These are called truthy & falsy values.  

Falsy Values: Values that are considered `false` in a Boolean context. These are:  

  - `false`  

  - `0`  

  - `""` (empty string)  

  - `null`  

  - `undefined`  

  - `NaN`  


Truthy Values: Values that are considered `true` in a Boolean context. These include:  

  - `true`  

  - Any non-zero number  

  - Non-empty strings  

  - Objects  

  - Arrays  

Example:  

let name = ""; // Empty string (falsy)  
if (name) {  
  console.log("Name is provided.");  
} else {  
  console.log("Name is not provided."); // This will execute  
}  
You can also try this code with Online Javascript Compiler
Run Code


In this example, the empty string `""` is falsy, so the `else` block runs.  

3. Boolean Object  

JavaScript also provides a `Boolean` object, which is a wrapper around the primitive Boolean data type. However, it’s rarely used because it can lead to confusion.  


Example:  

let boolObj = new Boolean(false); // Boolean object  
if (boolObj) {  
  console.log("This will run because objects are truthy.");  
}  


Here, even though `boolObj` wraps `false`, the object itself is truthy, so the `if` condition runs.  

4. Boolean Conversion  

You can convert other data types to Boolean using the `Boolean()` function. This is useful when you need to check if a value exists or not.  

Example:  

let age = 0;  
console.log(Boolean(age)); 
let name = "John";  
console.log(Boolean(name)); 
You can also try this code with Online Javascript Compiler
Run Code


 Output: 

false (0 is falsy)  
 true (non-empty string is truth)  

 

This conversion is often used in conditional statements to simplify checks.  

Everything With a “Value” is True

In JavaScript, any value that has content or a meaningful value is considered truthy. This includes strings, numbers, arrays, and objects, as long as they are not empty or undefined.

Example

let nonEmptyString = "Hello, World!";
let nonZeroNumber = 10;
let nonEmptyArray = [1, 2, 3];


console.log(Boolean(nonEmptyString));  
console.log(Boolean(nonZeroNumber));   
console.log(Boolean(nonEmptyArray));   
You can also try this code with Online Javascript Compiler
Run Code


Output: 

true
true
true


Explanation:

  • A non-empty string like "Hello, World!" is truthy.
     
  • Any non-zero number like 10 is considered true.
     
  • Arrays, even if they contain no elements, are also truthy.

Everything Without a “Value” is False

On the flip side, JavaScript considers values that are empty or undefined as falsy. These include 0, false, undefined, null, NaN, and an empty string "".

Example

let zero = 0;
let emptyString = "";
let notDefined;
let nothing = null;


console.log(Boolean(zero));        
console.log(Boolean(emptyString)); 
console.log(Boolean(notDefined)); 
console.log(Boolean(nothing));     
You can also try this code with Online Javascript Compiler
Run Code


Output: 

false
false
false
false


Explanation:

  • 0, "", null, and undefined are all falsy values in JavaScript, meaning they evaluate to false when used in a Boolean context.

Boolean Coercion

Boolean coercion occurs when JavaScript automatically converts values into Booleans during logical operations. This automatic conversion helps in evaluating conditions more effectively.

Example

let result = "Hello" && 5;
console.log(result);  

result = "" || 10;
console.log(result);  
You can also try this code with Online Javascript Compiler
Run Code

Output: 

5
10


Explanation:

  • The expression "Hello" && 5 evaluates to 5 because both values are truthy, and && returns the last truthy value.
     
  • The expression "" || 10 evaluates to 10 because "" is falsy, and || returns the first truthy value.

JavaScript Booleans as Objects

In JavaScript, Boolean values (true or false) are primitive types. However, they can also be represented as objects by using the Boolean constructor.

Example

let boolPrimitive = true;
let boolObject = new Boolean(false);

console.log(typeof boolPrimitive);  
console.log(typeof boolObject);     
You can also try this code with Online Javascript Compiler
Run Code


Output: 

boolean
object


Explanation:

  • boolPrimitive is a Boolean primitive type, while boolObject is an object created using the Boolean constructor.

Constructor and Instance

The Boolean() function can be used as a constructor to create Boolean objects. However, using it as an object is not commonly recommended because it behaves differently from a primitive Boolean.

Example

let boolObj1 = new Boolean(true);
let boolObj2 = new Boolean(false);
console.log(boolObj1 == true); 
console.log(boolObj2 == false); 
You can also try this code with Online Javascript Compiler
Run Code


Output:

false
false


Explanation:

  • When compared using ==, the objects do not behave like primitive Booleans. This is a subtle difference when using Boolean() as a constructor.

Truthy and Falsy Values

In JavaScript, some values are automatically considered truthy or falsy depending on their content. This is especially important when using if conditions, loops, or logical operators.

Truthy Values

  • All non-zero numbers
     
  • Non-empty strings
     
  • Objects
     
  • Arrays

Falsy Values

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

Example

if ("Hello") {
    console.log("This is truthy");
} else {
    console.log("This is falsy");
}
if (0) {
    console.log("This is truthy");
} else {
    console.log("This is falsy");
}
You can also try this code with Online Javascript Compiler
Run Code


Explanation:

  • The string "Hello" is truthy, so the first console.log prints "This is truthy".
     
  • The number 0 is falsy, so the second console.log prints "This is falsy".

JavaScript Boolean Methods  

JavaScript provides a few methods specifically for working with Booleans. These methods help in converting & manipulating Boolean values. Let’s understand them one by one with examples.  

1. `toString()` Method  

The `toString()` method converts a Boolean value to a string. This is useful when you need to display a Boolean value as text.  

Example:  

let isActive = true;  
let isActiveString = isActive.toString();  
console.log(isActiveString); 
console.log(typeof isActiveString); 
You can also try this code with Online Javascript Compiler
Run Code


Output:

true
 string  


Here, `isActive` is a Boolean (`true`), & `toString()` converts it to the string `"true"`.  

2. `valueOf()` Method  

The `valueOf()` method returns the primitive value of a Boolean. This is mostly used internally by JavaScript, but you can use it explicitly if needed.  

Example:  

let isOnline = new Boolean(true); // Boolean object  
let primitiveValue = isOnline.valueOf();  
console.log(primitiveValue); 
console.log(typeof primitiveValue); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

true
boolean  


In this example, `isOnline` is a Boolean object, & `valueOf()` extracts its primitive value (`true`).  

3. Using Booleans in Conditional Statements  

Booleans are often used in `if` statements, loops, & other control structures to make decisions.  

Example:  

let isRaining = true;  
if (isRaining) {  
  console.log("Take an umbrella."); // This will execute  
} else {  
  console.log("Enjoy the weather!");  
}  


Here, the `if` statement checks if `isRaining` is `true`. If it is, the first block runs; otherwise, the `else` block runs.  

4. Logical Operators with Booleans  

JavaScript provides logical operators like `&&` (AND), `||` (OR), & `!` (NOT) to work with Booleans. These operators are used to combine or invert Boolean values.  

Example:  

let hasTicket = true;  
let hasPassport = false;  

// AND operator  
if (hasTicket && hasPassport) {  
  console.log("You can board the flight.");  
} else {  
  console.log("You cannot board the flight."); // This will execute  
}  

// OR operator  
if (hasTicket || hasPassport) {  
  console.log("You might be able to board the flight."); // This will execute  
}  

// NOT operator  
if (!hasPassport) {  
  console.log("You need a passport to travel."); // This will execute  
}  


In this example:  

  • The `&&` operator checks if both conditions are `true`.  
     
  • The `||` operator checks if at least one condition is `true`.  
     
  • The `!` operator inverts the Boolean value.  

5. Boolean in Functions  

Booleans are often returned by functions to indicate success, failure, or other states.  

Example:  

function isEligible(age) {  
  return age >= 18;  
}  
let userAge = 20;  
console.log(isEligible(userAge)); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

true  


Here, the function `isEligible` returns `true` if the age is 18 or older, & `false` otherwise.  

Boolean Coercion in Conditions

Boolean coercion happens when JavaScript implicitly converts values to Booleans for conditional checks. It’s a key concept to understand when working with conditional statements like if and while.

Example

let value = 10;
if (value) {
    console.log("Value is truthy");
} else {
    console.log("Value is falsy");
}
You can also try this code with Online Javascript Compiler
Run Code


Explanation:

  • Since the value is 10 (a non-zero number), it is truthy, so "Value is truthy" is printed.

Practical Applications

Booleans are heavily used in conditional statements, loops, and logical operations. They help in making decisions within your code and controlling program flow.

Example: Using Booleans in a Function

function isEven(num) {
    return num % 2 === 0;
}
console.log(isEven(4)); 
console.log(isEven(7)); 
You can also try this code with Online Javascript Compiler
Run Code


Output: 

true
false


Explanation:

  • The isEven function returns true if the number is even and false if it is odd. This demonstrates how Booleans can be used to control program logic.

Frequently Asked Questions

What are truthy and falsy values in JavaScript?

In JavaScript, truthy values are values that evaluate to true in a Boolean context (like 1, non-empty strings, or objects). Falsy values are values that evaluate to false (like 0, false, null, undefined, or an empty string).

What is the Boolean() function in JavaScript?

The Boolean() function in JavaScript is used to convert other values into Boolean values (true or false). It’s a simple way to check if a value is truthy or falsy.

How are Booleans used in JavaScript?

Booleans are primarily used in conditional statements (e.g., if statements) to control the flow of your program. They are also involved in logical operations like && (AND), || (OR), and ! (NOT).

Conclusion

In this article, we learned the fundamentals of JavaScript Booleans. We discussed the Boolean() function, how values are evaluated as truthy or falsy, and how Boolean coercion works in conditions. Additionally, we explored practical examples and use cases where Booleans are essential in programming. 

Live masterclass