A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/javascript-boolean/ below:

JavaScript Boolean - GeeksforGeeks

JavaScript Boolean

Last Updated : 11 Jul, 2025

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));    
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"));

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)); 

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);

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");
}

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); 

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."

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

Truthy/Falsy in Action

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

Output
false
true
false
true
Boolean Coercion in Conditions JavaScript
let age = 0;
if (age) {
    console.log("Age provided");
} else {
    console.log("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");
}
2. Ternary Operator JavaScript
let age = 16;
let isAdult = age >= 18 ? true : false;
console.log(isAdult);
3. Logical Short-Circuiting JavaScript
let defaultName = "Guest";
let userName = "" || defaultName;
console.log(userName);

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.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4