⇧
vadnica-logo
Editor My Blog Password Manager Finance My Services About the Project Telegram O meni O meni

Boolean Values

Very often in programming we need a data type that can have only two values, and those two values are true and false. That is why JavaScript has a Boolean data type. With the Boolean() function we can determine whether an expression (or variable) is true or not. JavaScript statements enable comparison and checking of values using various operators (==, ===, >, <). With them, we can check value equality, compare numbers, and execute conditional statements (if, else).

EXAMPLE
RESULT

The result of every comparison is a Boolean value true or false. Typically, JavaScript Boolean values are primitive values created from literals. Boolean values can also be defined as objects with the new keyword, but the use of Boolean objects is discouraged, as it can lead to unexpected results when comparing values.

Item Description Syntax Example
Boolean values
true Logically true true let x = true;
false Logically false false let x = false;
Boolean() Converts a value to boolean Boolean(value) Boolean(0); // false
Falsy values (return false)
false, 0, -0, 0n Numeric zeros Boolean(0) Boolean(0); // false
"" Empty string Boolean("") Boolean(""); // false
null, undefined, NaN Empty or invalid values Boolean(null) Boolean(null); // false
Truthy values (return true)
Anything else Everything that is not falsy Boolean(value) Boolean("0"); // true
{}, [] Empty objects and arrays Boolean({}) Boolean([]); // true
Logical operators
&& AND (both must be true) a && b true && false; // false
|| OR (at least one must be true) a || b true || false; // true
! NOT (reverses the value) !a !true; // false
!! Double NOT (converts to boolean) !!a !!"hello"; // true
Comparison operators (return boolean)
===, !== Strict equal/not equal (type + value) a === b 5 === "5"; // false
==, != Equal/not equal (with type conversion) a == b 5 == "5"; // true
> , < , >= , <= Size comparison a > b 10 > 5; // true
Boolean object methods
constructor Returns the function that created the Boolean prototype. obj.constructor ▶ Code example
prototype Allows adding properties and methods to the Boolean prototype. Boolean.prototype ▶ Code example
toString() Converts a Boolean value to a string. bool.toString() ▶ Code example
valueOf() Returns the primitive value. bool.valueOf() ▶ Code example