JavaScript Boolean
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.
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.
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.
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.
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.
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.
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.
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
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
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
let score = 70;
if (score >= 50) {
console.log("Pass");
} else {
console.log("Fail");
}
Output
Pass
2. Ternary Operator
let age = 16;
let isAdult = age >= 18 ? true : false;
console.log(isAdult);
Output
false
3. Logical Short-Circuiting
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.
JavaScript Boolean – FAQs
What is a Boolean in JavaScript?
A Boolean is a primitive data type in JavaScript that can have one of two values: true or false. It is used to represent logical values and control the flow of the program.
What values are considered truthy or falsy in JavaScript?
- Falsy values: false, 0, -0, 0n, “” (empty string), null, undefined, NaN.
- Truthy values: All values that are not falsy, including objects, non-zero numbers, non-empty strings, and arrays.
How do you use Booleans in conditional statements?
Booleans are commonly used in conditional statements like if, else, while, and for loops to control the flow of the program.
How do you compare Boolean values?
You can compare Boolean values using standard comparison operators (==, !=, ===, !==). The strict equality operators (===, !==) are recommended to avoid type coercion.
How do Boolean objects differ from Boolean primitives?
Boolean objects are created using the Boolean constructor and are objects, while Boolean primitives are simply true or false. Boolean objects are always truthy, even if they represent false.