The Wayback Machine - https://web.archive.org/web/20240930161109/https://www.geeksforgeeks.org/javascript-boolean/
Open In App

JavaScript Boolean

Last Updated : 30 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

JavaScript Boolean represents true or false values. It’s used for logical operations, condition testing, and variable assignments based on conditions. Values like 0, NaN, empty strings, undefined, and null are false; non-empty strings, numbers other than 0, objects, and arrays are true.

Note: A variable or object which has a value is treated as a true boolean value. ‘0‘, ‘NaN’, empty string, ‘undefined’, and ‘null’ is treated as false boolean values. 

Here a1 and a2 store the boolean value i.e. true and false respectively. 

let a1 = true;
let a2 = false;

Note: The below variables are initialized with strings, not boolean values.  

let a1 ="true";
let a2 ="false";

Boolean() function in JavaScript

The Boolean() function in JavaScript converts any value to its corresponding Boolean representation: truthy values become true, and falsy values become false.

Syntax:  

Boolean(variable/expression) 

Example 1: The below program will give true values as output.

javascript
function gfg() { 
    console.log(Boolean(12));
} 
gfg();

Output
true

Example 2: Below program will give true values as output.

JavaScript
console.log('Boolean(10) is ' + Boolean(10));

console.log('Boolean("GeeksforGeeks") is '+ Boolean("GeeksforGeeks"));

console.log('Boolean(2.74) is ' + Boolean(2.74));

console.log('Boolean(-1) is ' + Boolean(-1));

console.log("Boolean('true') is " + Boolean('true'));

console.log("Boolean('false') is " + Boolean('false'));

console.log('Boolean(3 * 2 + 1.11) is '+ Boolean(3 * 2 + 1.11));

console.log('Boolean(1<2) is ' + Boolean(1 < 2));

Output
Boolean(10) is true
Boolean("GeeksforGeeks") is true
Boolean(2.74) is true
Boolean(-1) is true
Boolean('true') is true
Boolean('false') is true
Boolean(3 * 2 + 1.11) is true
Boolean(1<2) is true

Example 3: Below program will give false values as output. 

javascript
let e; //undefined
console.log('Boolean(0) is ' + Boolean(0));

console.log('Boolean("") is ' + Boolean(""));

console.log('Boolean(e) undefined is '+ Boolean(e));

console.log('Boolean(-0) is ' + Boolean(-0));

console.log('Boolean(false) is ' + Boolean(false));

console.log('Boolean(NaN) is ' + Boolean(NaN));

console.log('Boolean(null) is ' + Boolean(null));

console.log('Boolean(1>2) is ' + Boolean(1 > 2));

Output
Boolean(0) is false
Boolean("") is false
Boolean(e) undefined is false
Boolean(-0) is false
Boolean(false) is false
Boolean(NaN) is false
Boolean(null) is false
Boolean(1>2) is false

JavaScript Boolean object: 

The boolean object in javascript is an object wrapper for boolean values. Booleans in JavaScript can also be defined using the new keyword. 

Syntax:  

new Boolean(value)

Below are examples of the JavaScript Boolean method.

Example 1: Below program will give false values for the first 4 variables & true for last 2 values as output.

javascript
let v1 = false;
let v2 = new Boolean(false);
let v3 = new Boolean("");
let v4 = new Boolean(0);
let v5 = new Boolean(true);
let v6 = new Boolean("GeeksforGeeks");
console.log('v1 = ' + v1);
console.log('v2 = ' + v2);
console.log('v3 = ' + v3);
console.log('v4 = ' + v4);
console.log('v5 = ' + v5);
console.log('v6 = ' + v6);

Output
v1 = false
v2 = false
v3 = false
v4 = false
v5 = true
v6 = true

Example 2: Below program will give true for the first value & false for the second value as output.

javascript
let v1 = true;
let v2 = new Boolean(true);

console.log('v1 = = v2 is ' + (v1 == v2));
console.log('v1 = = = v2 is ' + (v1 === v2));

Output
v1 = = v2 is true
v1 = = = v2 is false

Note: v1 = = = v2 is not true as the type of v1 and v2(object) is not the same.

Supported Browsers

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.



Previous Article
Next Article

Similar Reads

Explain non-boolean value coercion to a boolean one in JavaScript
As we all know javascript is a forgiving language. It does not mind silly mistakes which programmers do. So, sometimes it gives unexpected results. So it senses Javascript says "I can do every possibility". Coercion is one of those in which javascript gives weird results because javascript automatically performs type conversion in which first value
3 min read
JavaScript Boolean valueOf() Method
The boolean.valueOf() method is used to return a boolean value either "true" or "false" depending upon the value of the specified boolean object. Syntax: boolean.valueOf() Parameter: This method does not accept any parameter. Return value: It returns a boolean value either "true" or "false" depending upon the value of the specified boolean object.
4 min read
JavaScript Boolean toString() Method
The boolean.toString() method is used to return a string either "true" or "false" depending upon the value of the specified boolean object. Syntax: boolean.toString() Parameter: This method does not accept any parameter. Return Values: It returns a string either "true" or "false" depending upon the value of the specified boolean object. Below are e
3 min read
JavaScript Boolean() Constructor
The Boolean() constructor in JavaScript is a built-in function that can be used to create a new Boolean object. The Boolean() constructor takes a single argument and returns a Boolean object with a value of true or false depending on the type and value of the argument passed in. Syntax: Boolean(value) Parameter: This constructor accepts a single ar
1 min read
JavaScript Boolean Constructor Property
JavaScript boolean constructor property returns the constructor function for an object. The function which is returned by this property is just the reference to this function, not a Boolean containing the function’s name The JavaScript number constructor, string constructor, and boolean constructor return function Boolean() { [native code] }, funct
1 min read
Convert boolean result into number/integer in JavaScript
A JavaScript boolean represents one of two values: true or false. However, if one wants to convert a variable that stores boolean value, into integer "0" or "1", they can do so using multiple approaches. We will look into some of them in this article. The most popular methods are: Using ternary or conditional operator Using unary + operator. Using
3 min read
JavaScript Boolean Reference
JavaScript Boolean is a datatype that returns either true or false In JavaScript, a boolean is used as a function to get the value of a variable, object, conditions, expressions, etc in terms of true or false Syntax: Boolean(variable/expression) Example: If the operands are equal, the equal to operator == returns true C/C++ Code <script> cons
2 min read
How to generate a random boolean using JavaScript ?
The task is to generate random boolean value. Here we are going to use JavaScript to achieve the goal. Approach 1: Calculate Math.random() function. If it is less than 0.5, then true otherwise false. Example 1: This example implements the above approach. <!DOCTYPE HTML> <html> <head> <title> How to generate a random boolean
2 min read
Implement a Function that Accept Array and Condition and Returns Boolean Values in JavaScript
In JavaScript language every function is a special function that checks the element of the array with the condition given in the input, Each element is checked against the condition, and if the condition is satisfied then every function returns the true result else if any of the element of the array fails against the condition then the false result
2 min read
How to convert Number to Boolean in JavaScript ?
We convert a Number to Boolean by using the JavaScript Boolean() method and double NOT operator(!!). A JavaScript boolean results in one of two values i.e. true or false. However, if one wants to convert a variable that stores integer “0” or “1” into Boolean Value i.e. "false" or "true".  Below are the approaches: Table of Content Boolean() methodU
2 min read
How to toggle a boolean using JavaScript ?
A boolean value can be toggled in JavaScript by using two approaches which are discussed below: Table of Content Using the logical NOT operatorUsing the ternary operatorUsing the XOR (^) operatorMethod 1: Using the logical NOT operator The logical NOT operator in Boolean algebra is used to negate an expression or value. Using this operator on a tru
3 min read
How to use the ! Operator to negate a Boolean Value in JavaScript ?
In JavaScript, you can use the ! (logical NOT) operator to negate a boolean value. The ! operator flips the truthiness of a value. If the operand is true, it becomes false, and if the operand is false, it becomes true. Example: Here, !isTrue evaluates to false, and !isFalse evaluates to true. C/C++ Code let isTrue = true; let isFalse = false; let n
1 min read
JavaScript Program for Boolean to String Conversion
This JavaScript program is designed to convert a boolean value to its corresponding string representation. In JavaScript, boolean values can either be true or false. The goal is to create a function that takes a boolean input and returns a string representation of that boolean value ("true" for true, and "false" for false). Below are the methods to
1 min read
JavaScript Convert a string to boolean
To Convert a string to Boolean, we have multiple approaches. We are going to learn how to Convert a string to Boolean. Below are the approaches used to Convert a string to Boolean: Table of Content Approach 1: Using JavaScript == OperatorApproach 2: Using JavaScript === OperatorApproach 3: Using the Boolean() functionApproach 4: Using a regular exp
5 min read
Sort an array of objects using Boolean property in JavaScript
Given the JavaScript array containing Boolean values. The task is to sort the array on the basis of Boolean value with the help of JavaScript. There are two approaches that are discussed below: Table of Content Using Array.sort() Method and === OperatorUsing Array.sort() and reverse() MethodsUsing a Custom Comparator Function for ObjectsUsing Array
2 min read
How to initialize a boolean array in JavaScript ?
A Boolean array is a collection of variables that can hold only two values: true or false. In JavaScript, Boolean values represent the simplest forms of truthiness and are essential for controlling flow in your programs, such as in conditional statements and loops. Why Use Boolean Arrays?Boolean arrays are useful when you need to track binary state
3 min read
How to convert string to boolean in PHP?
Given a string and the task is to convert given string to its boolean. Use filter_var() function to convert string to boolean value. Examples: Input : $boolStrVar1 = filter_var('true', FILTER_VALIDATE_BOOLEAN); Output : true Input : $boolStrVar5 = filter_var('false', FILTER_VALIDATE_BOOLEAN); Output : false Approach using PHP filter_var() Function:
2 min read
p5.js boolean() function
The boolean() function in p5.js is used to convert the given string and number value into its boolean representation. Syntax: boolean(Value) Parameters: This function accepts single parameter Value which are to be converted into its boolean representation. This value might be integer, float, string, boolean, negative or positive value and array of
3 min read
ES6 Boolean
The Boolean of ES6 is nothing different than the other Boolean objects. It also represents two values True or False. There are two properties and three methods that build this object in ES6 JavaScript. Properties of Boolean Object: JavaScript constructor: In ES6 JavaScript, the constructor property returns the constructor function for an object. Fo
3 min read
SASS | Booleans and Boolean operators
SASS supports boolean values: true false It is used in conditional compilation where the expression evaluates to either of these values. Assign a boolean value to the variable: $variable: true; or $variable: false; Usage in Conditional Compilation: We can use boolean values in conditional compilation. See the example below, we have passed a true va
2 min read
How to validate if input in input field has boolean value using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In a certain input field, only boolean value i.e. true or false are allowed. We can also validate these input fields to accept onl
4 min read
Primer CSS Boolean Attributes
Primer CSS is a free open-source CSS framework that is built upon systems that create the foundation of the basic style elements such as spacing, typography, and color. This systematic method makes sure our patterns are steady and interoperable with every other. Its approach to CSS is influenced by object-oriented CSS principles, functional CSS, an
2 min read
Angular PrimeNG Form Checkbox Boolean Value Component
Angular PrimeNG is a UI component library for Angular Applications. It offers many pre-built themes and UI components for a variety of tasks like inputs, menus, charts, Buttons, etc. In this article, we will be seeing the Form Checkbox Boolean Value Component in Angular PrimeNG. The Checkbox component provided by PrimeNG is an extension of the HTML
3 min read
Less.js Logical boolean() Function
Less.js is a simple CSS pre-processor that facilitates the creation of manageable, customizable, and reusable style sheets for websites. It is a dynamic style sheet language that improves CSS's functionality. Cross-browser interoperability is supported by LESS. A scripting language known as CSS pre-processor is used to enhance CSS and compile it in
3 min read
TypeScript Primitives: String, Number, and Boolean Type
In this article, we are going to learn about TypeScript primitives: string, number, and boolean Type in Typescript. These are the most used and basic data types. Below we will learn about string, number, and boolean in detail. TypeScript Primitive DataTypesSimilar to JavaScript's mostly used data types TypeScript has mainly 3 primitives String: Typ
2 min read
How to Convert String to Boolean in TypeScript ?
In Typescript, sometimes you receive the data as strings but need to work with boolean values or identify the boolean equivalent of it. There are several approaches to convert string to boolean in TypeScript which are as follows: Table of Content Using Conditional StatementUsing JSON.parse() MethodUsing Type AssertionUsing Regular ExpressionsUsing
4 min read
Boolean Data Type
In programming languages, we have various data types to store different types of data. Some of the most used data types are integer, string, float, and boolean. The boolean data type is a type of data that stores only two types of values i.e. True or False. These values are not case-sensitive depending upon programming languages. The name Boolean c
13 min read
JavaScript Course Understanding Code Structure in JavaScript
Inserting JavaScript into a webpage is much like inserting any other HTML content. The tags used to add JavaScript in HTML are <script> and </script>. The code surrounded by the <script> and </script> tags is called a script blog. The 'type' attribute was the most important attribute of <script> tag. However, it is no
4 min read
Introduction to JavaScript Course - Learn how to build a task tracker using JavaScript
This is an introductory course about JavaScript that will help you learn about the basics of javascript, to begin with, the dynamic part of web development. You will learn the basics like understanding code structures, loops, objects, etc. What this course is about? In this course we will teach you about the basics of the scripting language i.e) Ja
4 min read
JavaScript Course Loops in JavaScript
Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. For example, suppose we want to print “Hello World” 10 times. Example: In this example we will print the same things, again and again, to understand the work of Loops. <script> cons
4 min read