English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Basic Tutorial

JavaScript Object

JavaScript Function

JS HTML DOM

JS Browser BOM

AJAX Basic Tutorial

JavaScript Reference Manual

JavaScript Boolean (Boolean) Object

In computer science, boolean values are a logical data type that can only havetrueorfalsevalue.

For this, JavaScript hasbooleandata type. It can only taketrueorfalsevalue.

let x = true;
let y = false;
Test to see‹/›

Boolean() function

The Boolean() function can be used to check if an expression istrue:

Boolean(10 < 22) // Returns true
Test to see‹/›

Alternatively, you can also use the following syntax:

(5 > 10)  // Returns false
(5 < 10)  // Returns true
("Apple" === "Apple") // Returns true
Test to see‹/›

comparisons and conditions

In JavaScript, boolean conditions are typically used to determine which parts of the code to execute (for examplein an if statement) or executed repeatedly (for examplein a for loop).

Below is some JavaScript pseudocode (not actual executable code) that demonstrates this concept.

  /* JavaScript if statement */
  if (boolean conditional) {
   // Code executed when the condition is true
  }
  
  if (boolean conditional) {
   console.log("The result of the boolean condition check is true");
  } else {
   console.log("The result of the boolean condition check is false");
  }
  
  
  /* JavaScript for loop */
  for (control variable; boolean conditional; counter) {
  // Code that is executed repeatedly if the condition is true
  }
  
  for (var i = 0; i < 4; i++) {
  console.log("Print only when the boolean condition is true");
  }

Here areBoolean conditionsSome examples:

OperatorDescriptionExample
==equal toif (month == "July")
>greater thanif (age > 18)
<less thanif (age < 18)

Everything with 'value' is true

All values, including any object or string "false", will create an initial valuetrue.

Boolean(50);  // Returns true
Boolean(-50); // Returns true
Boolean(3.14);// Returns true
Boolean("Hello"); // Returns true
Boolean("false"); // Returns true
Test to see‹/›

Everything without 'value' is false

If a value is omitted or is 0,-If an object's initial value is 0, null, false, NaN, undefined, or an empty string (""), then the object's initial value isfalse.

The boolean value 0 (zero) isfalse:

var x = 0;
Boolean(x); // Returns false
Test to see‹/›

boolean-0 (minus zero) isfalse:

var x = -0;
Boolean(x); // Returns false
Test to see‹/›

nullThe boolean value isfalse:

var x = null;
Boolean(x); // Returns false
Test to see‹/›

falseThe boolean value isfalse:

var x = false;
Boolean(x); // Returns false
Test to see‹/›

NaNThe boolean value isfalse:

var x = 50 / "Parrot";   // x will be NaN (Not a Number)
Boolean(x);  // Returns false
Test to see‹/›

undefinedThe boolean value isfalse:

var x;
Boolean(x); // Returns false
Test to see‹/›

The boolean value of an empty string ("") isfalse:

var x = "";
Boolean(x); // Returns false
Test to see‹/›

Boolean primitives and boolean objects

Generally, JavaScript boolean values are created from literal values:

var x = false;

But, you can also use the new keyword to define a boolean value as an object:

var x = new Boolean(false);

To test the difference between the two, we will initialize a boolean primitive and a boolean object.

var x = false;
var y = new Boolean(false);
typeof x// Returns boolean
typeof y// Returns object
Test to see‹/›

Note:Do not create boolean values as objects. This will reduce execution speed and may produce some unexpected results.

When using the == operator, equal boolean values are equal:

var x = false;
var y = new Boolean(false);
document.write(x == y); // Returns true because x and y have equal values
Test to see‹/›

When using the === operator, equal boolean values are not equal because the === operator expects both value and type to be equal:

var x = false;
var y = new Boolean(false);
document.write(x === y); // Returns false because x and y have different types
Test to see‹/›

Objects cannot be compared:

var x = new Boolean(false);
var y = new Boolean(false);
document.write(x == y); // Because x and y are different objects, the return value is false
document.write(x === y); // Because x and y are different objects, the return value is false
Test to see‹/›

Note the difference between (==) and (===). Comparing two JavaScript objects will always return false.

Complete Boolean Reference

For a complete reference to properties and methods, please visit ourJavaScript Boolean Reference.

The reference section includes descriptions and examples of all boolean properties and methods.