English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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‹/›
The Boolean() function can be used to check if an expression istrue:
Boolean(10 < 22) // Returns trueTest to see‹/›
Alternatively, you can also use the following syntax:
(5 > 10) // Returns false (5 < 10) // Returns true ("Apple" === "Apple") // Returns trueTest to see‹/›
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:
Operator | Description | Example |
---|---|---|
== | equal to | if (month == "July") |
> | greater than | if (age > 18) |
< | less than | if (age < 18) |
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 trueTest to see‹/›
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 falseTest to see‹/›
boolean-0 (minus zero) isfalse:
var x = -0; Boolean(x); // Returns falseTest to see‹/›
nullThe boolean value isfalse:
var x = null; Boolean(x); // Returns falseTest to see‹/›
falseThe boolean value isfalse:
var x = false; Boolean(x); // Returns falseTest to see‹/›
NaNThe boolean value isfalse:
var x = 50 / "Parrot"; // x will be NaN (Not a Number) Boolean(x); // Returns falseTest to see‹/›
undefinedThe boolean value isfalse:
var x; Boolean(x); // Returns falseTest to see‹/›
The boolean value of an empty string ("") isfalse:
var x = ""; Boolean(x); // Returns falseTest to see‹/›
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 objectTest 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 valuesTest 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 typesTest 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 falseTest to see‹/›
Note the difference between (==) and (===). Comparing two JavaScript objects will always return false.
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.