• Home
  • Jobs
  • Courses
  • Certifications
  • Companies
  • Online IDE
  • Login
  • Signup
MYTAT
  • Home
  • Jobs
  • Courses
  • Certifications
  • Companies
  • Online IDE
  • Login
  • Signup
JavaScript
  • JavaScript Introduction
  • JavaScript Where To
  • JavaScript Output
  • JavaScript Statements
  • JavaScript Syntax
  • JavaScript Comments
  • JavaScript Variables
  • JavaScript Let
  • JavaScript Const
  • JavaScript Operators
  • JavaScript Assignment
  • JavaScript Data Types
  • JavaScript Functions
  • JavaScript Objects
  • JavaScript Events
  • JavaScript Strings
  • JavaScript String Methods
  • JavaScript Numbers
  • JavaScript Number Methods
  • JavaScript Arrays
  • JavaScript Array Const
  • JavaScript Array Methods
  • JavaScript Sorting Arrays
  • JavaScript Array Iteration
  • JavaScript Date Objects
  • JavaScript Date Formats
  • JavaScript Get Date Methods
  • JavaScript Set Date Methods
  • JavaScript Math Object
  • JavaScript Random
  • JavaScript Booleans
  • JavaScript Comparison And Logical Operators
  • JavaScript If Else And Else If
  • JavaScript Switch Statement
  • JavaScript For Loop
  • JavaScript Break And Continue
  • JavaScript Type Conversion
  • JavaScript Bitwise Operations
  • JavaScript Regular Expressions
  • JavaScript Errors
  • JavaScript Scope
  • JavaScript Hoisting
  • JavaScript Use Strict
  • The JavaScript This Keyword
  • JavaScript Arrow Function
  • JavaScript Classes
  • JavaScript JSON
  • JavaScript Debugging
  • JavaScript Style Guide
  • JavaScript Common Mistakes
  • JavaScript Performance
  • JavaScript Reserved Words
  • JavaScript Versions
  • JavaScript History
  • JavaScript Forms
  • JavaScript Validation API
  • JavaScript Objects
  • JavaScript Object Properties
  • JavaScript Function Definitions
  • JavaScript Function Parameters
  • JavaScript Function Invocation
  • JavaScript Closures
  • JavaScript Classes
  • Java Script Async
  • JavaScript HTML DOM
  • The Browser Object Model
  • JS Ajax
  • JavaScript JSON
  • JavaScript Web APIs
  • JS Vs JQuery
  • Home
  • Courses
  • JavaScript
  • JavaScript Booleans

JavaScript Booleans

Previous Next

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 now

Or even easier:

Example

(10 > 9)              // also returns true
10 > 9                // also returns true
Try it now
 

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 now

Everything Without a "Value" is False

The Boolean value of 0 (zero) is false:

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

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

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

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

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

The Boolean value of undefined is false:

let x;
Boolean(x);       // returns false
Try it now

The Boolean value of null is false:

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

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

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

The Boolean value of NaN is false:

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

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
 

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

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

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

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



Practice Excercise Practice now

Previous Next
COMPANY
  • About us
  • Careers
  • Contact Us
  • In Press
  • People
  • Companies List
Products
  • Features
  • Coding Assessments
  • Psychometric Assessment
  • Aptitude Assessments
  • Tech/Functional Assessments
  • Video Assessment
  • Fluency Assessment
  • Campus
 
  • Learning
  • Campus Recruitment
  • Lateral Recruitment
  • Enterprise
  • Education
  • K 12
  • Government
OTHERS
  • Blog
  • Terms of Services
  • Privacy Policy
  • Refund Policy
  • Mart Category
Partner
  • Partner Login
  • Partner Signup

Copyright © RVR Innovations LLP 2025 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP