Lesson 13 of 30 – JavaScript Booleans
43%

JavaScript Booleans

Boolean is a primitive data type in JavaScript that can store only two values: true and false. Boolean values are mainly used for making decisions in programs such as if statements, loops, and comparisons.

Note: A boolean value always represents either true or false.
What are Boolean Values?

JavaScript provides two boolean values:

  • true
  • false
Example:
var YES = true;

var NO = false;
Boolean with if Statement

Boolean values control program execution.

var YES = true;

if(YES){
alert("Executed");
}

Since YES is true, the code executes.

Comparison Operators

Comparison operations return boolean values.

var a=10;
var b=20;

a<b; // true

a>b; // false

Comparison results are always true or false.

Boolean() Function

JavaScript provides the Boolean() function to convert values into true or false.

Example:
Boolean("Hello"); // true

Boolean(10); // true

Boolean([]); // true

Boolean(100+50); // true

Any non-empty string, non-zero number, array, or object becomes true.

Values that Become False

The following values convert to false:

Boolean(""); // false

Boolean(0); // false

Boolean(null); // false

Boolean(undefined); // false

Boolean(NaN); // false
  • 0
  • -0
  • null
  • false
  • undefined
  • NaN
  • Empty string ""
Boolean Object

Using the new keyword creates a Boolean object.

var bool = new Boolean(true);

alert(bool);

The output will be true.

Boolean Object in Condition

A Boolean object always evaluates to true inside a condition.

var bool = new Boolean(false);

if(bool){
alert("Executed");
}

Even though the value is false, the object itself is treated as true.

Boolean vs boolean

Lowercase boolean is a primitive type. Uppercase Boolean is an object.

var b1 = new Boolean(true);

var b2 = true;

typeof b1; // object

typeof b2; // boolean
Boolean Methods

JavaScript Boolean values provide the following methods.

Method Description
toLocaleString() Returns the Boolean value as a local string.
toString() Converts a Boolean value into a string.
valueOf() Returns the primitive Boolean value.
Advantages of Boolean
  • Controls program flow.
  • Used in decision making.
  • Required for loops and conditions.
  • Makes code simple and readable.
  • Useful for validation and comparisons.
Key Points
  • Boolean has only two values: true and false.
  • Boolean is a primitive data type.
  • Comparison operators return Boolean values.
  • Boolean() converts values into true or false.
  • Empty string, 0, null and undefined become false.
  • Boolean objects are different from primitive booleans.
  • Use typeof to identify Boolean types.
Quick Revision
Value Boolean Result
true true
false false
0 false
"" false
"Hello" true
100 true
Best Practices
  • Use primitive boolean values whenever possible.
  • Avoid creating Boolean objects with new Boolean().
  • Use meaningful variable names like isActive and isLoggedIn.
  • Keep conditions simple and readable.
  • Use Boolean() for explicit type conversion.

🧠 Quick Quiz

Which of the following values returns false using Boolean()?