JavaScript Booleans

In this tutorial, you will learn about JavaScript booleans with the help of examples.

In JavaScript, booleans are the primitive data types that can either be true or false. For example,

const a = true;
const b = false;

Note: If you wrap true or false in a quote, then they are considered as a string.

For example,

const a = 'true';
console.log(typeof a); // string

The boolean values are mostly used for Comparison and Logical Operators. For example,

Equal to operator == returns true if the operands are equal.

console.log(5 == 6); // false

Not equal to operator != returns true if all the operands are not equal.

console.log(5 != 6); // true

Logical AND && returns true if both the operand values are true, else evaluates to false.

console.log(true && false); // false

The boolean values are used in if...else statements and for loops as well.

Here's a list of values that gets converted to specific boolean values.

Data type Boolean Value
undefined false
null false
NaN false
'' false
0 false
20 true
-20 true
'hello' true

JavaScript Boolean Methods

Here is a list of built-in boolean methods in JavaScript.

Method Description
toString() returns a boolean value by converting boolean to a string
valueOf() returns the primitive value of a boolean

Example: Using toString()

let count = false;

// converting to string
let result = count.toString();

console.log(result);
console.log(typeof result);

Output

false
string

Example: Using valueOf()

let count = true;

// converting to string
let result = count.valueOf();

console.log(result);

console.log(typeof result);

Output

true
boolean

JavaScript Boolean() Function

The Boolean() function is used to convert various data types to boolean values. For example,

const a = true;
console.log(Boolean(a)); // true

Everything with a value returns true. For example,

let result;
result = 20;
console.log(Boolean(result)); // true
console.log(typeof Boolean(result)); // boolean

result = -20;
console.log(Boolean(result)); // true

result = 'hello';
console.log(Boolean(result)); // true

result = {a: 1};
console.log(Boolean(result)); // true

In JavaScript, undefined, null, 0, NaN, '' converts to false. For example,

let result;
// empty string
result = Boolean('');
console.log(result); // false

result = Boolean(0);
console.log(result); // false

result = Boolean(undefined);
console.log(result); // false

result = Boolean(null);
console.log(result); // false

result = Boolean(NaN);
console.log(result); // false

Note: If you want to learn more about the boolean conversion, visit JavaScript Type Conversion.


Boolean Objects

You can also create a boolean value using the new keyword. For example,

const a = true;

// creating a boolean object
const b = new Boolean(true);

console.log(a); // true
console.log(b); // true

console.log(typeof a); // "boolean"
console.log(typeof b); // "object"

Note: It is recommended to avoid using boolean objects. Using boolean objects slows down the program.

Did you find this article helpful?