Tutorials References Exercises Menu
Paid Courses

JS Tutorial

JS HOME JS Introduction JS Where To JS Output JS Statements JS Syntax JS Comments JS Variables JS Let JS Const JS Operators JS Arithmetic JS Assignment JS Data Types JS Functions JS Objects JS Events JS Strings JS String Methods JS String Search JS String Templates JS Numbers JS Number Methods JS Arrays JS Array Methods JS Array Sort JS Array Iteration JS Array Const JS Dates JS Date Formats JS Date Get Methods JS Date Set Methods JS Math JS Random JS Booleans JS Comparisons JS Conditions JS Switch JS Loop For JS Loop For In JS Loop For Of JS Loop While JS Break JS Typeof JS Type Conversion JS Bitwise JS RegExp JS Errors JS Scope JS Hoisting JS Strict Mode JS this Keyword JS Arrow Function JS Classes JS JSON JS Debugging JS Style Guide JS Best Practices JS Mistakes JS Performance JS Reserved Words

JS Versions

JS Versions JS 2009 (ES5) JS 2015 (ES6) JS 2016 JS 2017 JS 2018 JS IE / Edge JS History

JS Objects

Object Definitions Object Properties Object Methods Object Display Object Accessors Object Constructors Object Prototypes Object Reference Object Map() Object Set()

JS Functions

Function Definitions Function Parameters Function Invocation Function Call Function Apply Function Closures

JS Classes

Class Intro Class Inheritance Class Static

JS Async

JS Callbacks JS Asynchronous JS Promises JS Async/Await

JS HTML DOM

DOM Intro DOM Methods DOM Document DOM Elements DOM HTML DOM Forms DOM CSS DOM Animations DOM Events DOM Event Listener DOM Navigation DOM Nodes DOM Collections DOM Node Lists

JS Browser BOM

JS Window JS Screen JS Location JS History JS Navigator JS Popup Alert JS Timing JS Cookies

JS Web APIs

Web API Intro Web Forms API Web History API Web Storage API Web Worker API Web Fetch API Web Geolocation API

JS AJAX

AJAX Intro AJAX XMLHttp AJAX Request AJAX Response AJAX XML File AJAX PHP AJAX ASP AJAX Database AJAX Applications AJAX Examples

JS JSON

JSON Intro JSON Syntax JSON vs XML JSON Data Types JSON Parse JSON Stringify JSON Objects JSON Arrays JSON Server JSON PHP JSON HTML JSON JSONP

JS vs jQuery

jQuery Selectors jQuery HTML jQuery CSS jQuery DOM

JS Examples

JS Examples JS HTML DOM JS HTML Input JS HTML Objects JS HTML Events JS Browser JS Editor JS Exercises JS Quiz JS Certificate

JS References

JavaScript Objects HTML DOM Objects


JavaScript Booleans


A JavaScript Boolean represents one of two values: true or false.


Boolean Values

Very often, in programming, you will need a data type that can only have one of two values, like

  • YES / NO
  • ON / OFF
  • TRUE / FALSE

For this, JavaScript has a Boolean data type. It can only take the values true or false.


The Boolean() Function

You can use the Boolean() function to find out if an expression (or a variable) is true:

Example

Boolean(10 > 9)        // returns true
Try it Yourself »

Or even easier:

Example

(10 > 9)              // also returns true
10 > 9                // also returns true
Try it Yourself »

Comparisons and Conditions

The chapter JS Comparisons gives a full overview of comparison operators.

The chapter JS Conditions gives a full overview of conditional statements.

Here are some examples:

Operator Description Example
== equal to if (day == "Monday")
> greater than if (salary > 9000)
< less than if (age < 18)

The Boolean value of an expression is the basis for all JavaScript comparisons and conditions.



Everything With a "Value" is True

Examples

100

3.14

-15

"Hello"

"false"

7 + 1 + 3.14
Try it Yourself »

Everything Without a "Value" is False

The Boolean value of 0 (zero) is false:

let x = 0;
Boolean(x);       // returns false
Try it Yourself »

The Boolean value of -0 (minus zero) is false:

let x = -0;
Boolean(x);       // returns false
Try it Yourself »

The Boolean value of "" (empty string) is false:

let x = "";
Boolean(x);       // returns false
Try it Yourself »

The Boolean value of undefined is false:

let x;
Boolean(x);       // returns false
Try it Yourself »

The Boolean value of null is false:

let x = null;
Boolean(x);       // returns false
Try it Yourself »

The Boolean value of false is (you guessed it) false:

let x = false;
Boolean(x);       // returns false
Try it Yourself »

The Boolean value of NaN is false:

let x = 10 / "Hallo";
Boolean(x);       // returns false
Try it Yourself »

Booleans Can be Objects

Normally JavaScript booleans are primitive values created from literals:

let x = false;

But booleans can also be defined as objects with the keyword new:

let y = new Boolean(false);

Example

let x = false;
let y = new Boolean(false);

// typeof x returns boolean
// typeof y returns object
Try it yourself »

Do not create Boolean objects. It slows down execution speed.
The new keyword complicates the code. This can produce some unexpected results:

When using the == operator, equal booleans are equal:

Example

let x = false;             
let y = new Boolean(false);

// (x == y) is true because x and y have equal values
Try it Yourself »

When using the === operator, equal booleans are not equal, because the === operator expects equality in both type and value.

Example

let x = false;             
let y = new Boolean(false);

// (x === y) is false because x and y have different types
Try it Yourself »

Or even worse. Objects cannot be compared:

Example

let x = new Boolean(false);             
let y = new Boolean(false);

// (x == y) is false because objects cannot be compared
Try it Yourself »

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


Complete Boolean Reference

For a complete reference, go to our Complete JavaScript Boolean Reference.

The reference contains descriptions and examples of all Boolean properties and methods.