Open In App

JavaScript Boolean

Last Updated : 11 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To represent logical values, JavaScript uses the Boolean data type, which has two possible values: true or false. These values often result from comparisons or logical operations. Additionally, the Boolean() function can convert other types of values into Boolean, determining their truthy or falsy nature.

The Boolean() Function

The Boolean() function is used to explicitly convert a value to its Boolean equivalent. Truthy values return true, while falsy values return false.

JavaScript
console.log(Boolean("Hello"));
console.log(Boolean(0));    

Output
true
false

Everything With a “Value” is True

In JavaScript, all objects, arrays, and non-empty strings are considered truthy values.

JavaScript
console.log(Boolean({}));
console.log(Boolean([])); 
console.log(Boolean("Hi"));

Output
true
true
true

Objects and arrays always evaluate to true in Boolean contexts.

Everything Without a “Value” is False

Certain values, like 0, null, undefined, NaN, and empty strings, are falsy.

JavaScript
console.log(Boolean("") === false); 
console.log(Boolean(undefined)); 

Output
true
false

These values lack meaningful content and thus evaluate to false.

JavaScript Booleans as Objects

While JavaScript supports primitive Booleans, the Boolean constructor can create Boolean objects. However, these objects are truthy regardless of their value.

JavaScript
let obj = new Boolean(false);
console.log(obj);      
console.log(typeof obj); 
console.log(Boolean(obj));

Output
[Boolean: false]
object
true

Using new Boolean() is generally discouraged because it can lead to confusion.

Boolean Primitives

A Boolean primitive is a simple representation of true or false. This is the most commonly used Boolean type in JavaScript.

JavaScript
let isAvailable = true;
console.log(typeof isAvailable);

Output
boolean

Boolean primitives are lightweight and efficient for logical operations.

Boolean Coercion

JavaScript implicitly converts values to Boolean in contexts like conditions and loops.

JavaScript
let user = "";
if (user) {
    console.log("Valid input");
} else {
    console.log("Invalid input");
}

Output
Invalid input

Empty strings evaluate to false, so “Invalid input” is logged.

Constructor and Instance

The Boolean constructor can create Boolean objects, but primitive Booleans are preferred.

JavaScript
let primitiveBool = true;
let objBool = new Boolean(false);
console.log(typeof primitiveBool);
console.log(typeof objBool); 

Output
boolean
object

Primitive Booleans are easier to use and avoid unnecessary complexity.

Truthy and Falsy Values

Falsy Values: Values that are evaluated as false when used in a Boolean. Unlike truthy values, falsy values represent “nothingness,” “emptiness,” or “failure.”

  • false
  • 0, -0
  • null
  • undefined
  • NaN
  • ” (empty string)

Truthy Values: Values that are evaluated to be true when used in a Boolean context, such as in conditional statements or logical operations.

  • Non-zero numbers (e.g., 1, -42)
  • Non-empty strings (e.g., ‘Hello’, ‘ ‘)
  • Objects and arrays ({}, [])

Truthy/Falsy in Action

JavaScript
console.log(Boolean(""));    
console.log(Boolean("text"));
console.log(Boolean(0));    
console.log(Boolean([]));  

Output
false
true
false
true
  • Empty strings and 0 are falsy.
  • Non-empty strings and arrays are truthy.

Boolean Coercion in Conditions

JavaScript
let age = 0;
if (age) {
    console.log("Age provided");
} else {
    console.log("Age missing");
}

Output
Age missing

Since userAge is 0, it evaluates to falsy, and “Age missing” is logged.

Practical Applications

1. Conditional Statements

JavaScript
let score = 70;
if (score >= 50) {
    console.log("Pass");
} else {
    console.log("Fail");
}

Output
Pass

2. Ternary Operator

JavaScript
let age = 16;
let isAdult = age >= 18 ? true : false;
console.log(isAdult);

Output
false

3. Logical Short-Circuiting

JavaScript
let defaultName = "Guest";
let userName = "" || defaultName;
console.log(userName);

Output
Guest

We have a Cheat Sheet on Javascript where we covered all the important topics of Javascript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.



Next Article

Similar Reads