CodesCompiler
πŸ”
HTMLCSSJavaScriptPythonSQLPHPSEOPostsBooksKids About Contact πŸš€ Get Started Free

JavaScript - Boolean

JavaScript - The Boolean Object

The JavaScriptBooleanobject represents two values, either β€œtrue” or β€œfalse”. You can create a boolean object using the Boolean() constructor with a β€˜new’ keyword. It takes a value as parameter and returns a boolean object. If value parameter is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. In programming, the if-else statement evaluates either the code of the β€˜if’ block or the β€˜else’ block based on the boolean value of the conditional expression.

Syntax

Use the following syntax to create a boolean object.

JAVASCRIPT CODE
const val = new Boolean(value);

Here value is an expression to convert into a Boolean object.

It returns an object containing the boolean value.

You can create a boolean primitive in JavaScript by assigning a boolean value to a variable βˆ’

JAVASCRIPT CODE
let bool = true;

Boolean Properties

Here is a list of the properties of Boolean object βˆ’

Sr.No.Property & Description1constructorReturns a reference to the Boolean function that created the object.2prototypeThe prototype property allows you to add properties and methods to an object.

In the following sections, we will have a few examples to illustrate the properties of Boolean object.

Boolean Methods

Here is a list of the methods of Boolean object and their description.

Sr.No.Method & Description1toSource()Returns a string containing the source of the Boolean object; you can use this string to create an equivalent object.2toString()Returns a string of either β€œtrue” or β€œfalse” depending upon the value of the object.3valueOf()Returns the primitive value of the Boolean object.

In the following sections, we will have a few examples to demonstrate the usage of the Boolean methods.

Example: Creating a Boolean Object

In the example below, we have defined the boolObj variable and stored the boolean object.

We used the typeof operator to check the type boolObj variable. In the output, you can observe that the type of the boolObj is the object.

Preview

Output

JAVASCRIPT CODE
typof boolObj == object

JavaScript Boolean() Function

The Boolean() function allows the developer to get the boolean value after evaluating the particular expression passed as a parameter.

JAVASCRIPT CODE
Boolean(Expression);

Here value is an expression to evaluate and get related boolean values. The Boolean() function returns the true or false based on the expression passed as a parameter.

Example

In the example below, We used the Boolean() function and passed different expressions as a parameter. In the output, you can observe the boolean value returned by the Boolean() function.

Preview

Output

JAVASCRIPT CODE
Boolean(100 > 90) : true
Boolean(100 < 90) : false
100 == 90 : false

JavaScript Falsy Boolean Values

The Boolean() function returns false for the falsy values. There are six falsy values false, null, undefined, 0 (zero), "" (empty string), NaN.

Let’s look at the example below.

Example

In the code below, we have passed all the falsy values as a parameter of the Boolean() function and printed the returned value from the Boolean() function. The Boolean() function returns false for all 7 values.

Preview

Output

JAVASCRIPT CODE
Boolean(0) : false
Boolean(-0) : false
Boolean(null) : false
Boolean(undefined) : false
Boolean('') : false
Boolean(NaN) : false
Boolean(false) : false

All other values are truthy, and the Boolean() function returns true.

Example

In the below code, we passed the truthy values as a Boolean() function parameter. The Boolean() function returns true for all truthy values. Even if we have passed the object and function as a Boolean() function parameter, it returns true.

Preview

Output

JAVASCRIPT CODE
Boolean(1) : true
Boolean(-1) : true
Boolean('Hello') : true
Boolean(true) : true
Boolean(10.99) : true
Boolean({name: 'John'}) : true
Boolean(() => {return 1;}) : true