JavaScript Booleans


A boolean data type returns either one of two values, namely true or false. Boolean is a function that can be used in JavaScript to determine whether a variable, object, condition, expression, etc. has a true or false value.

Example

Preview

JavaScript's Boolean() function

The result of a boolean function is the variable's boolean value. It may also be employed to determine the boolean outcome of a condition, expression, etc.

Note: It should be noted that a variable or object with value is considered as a true boolean value. False boolean values are regarded as "0," "NaN," "empty string," "undefined," and "null."

JavaScripts that demonstrate how boolean values function 

Code #1: The program below will output true values.

Example

Preview

Code #2: The program below will output false values.

Example

Preview

JavaScript Boolean Object

A boolean value is wrapped in an object by a boolean object in JavaScript. The new keyword in JavaScript may also be used to define booleans.

Boolean new syntax (value)

Note: Because the types of v1 and v2 (objects) are different, the statement v1 = == v2 is false.

Example

Preview