DEV Community

vidhya murali
vidhya murali

Posted on

JavaScript Booleans

The Boolean Data Type

In JavaScript, a Boolean is a primitive data type that can only have one of two values:

true or false

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

Key Boolean Characteristics

  • true and false are boolean data types
  • true and false are the only possible boolean values
  • true and false must be written in lowercase
  • true and false must be written without quotes

Boolean Use Cases

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

  • yes or no
  • on or off
  • true or false

Boolean values are fundamental for logical operations and control flow in JavaScript programming.

Comparisons

All JavaScript comparison operators (like ==, !=, <, >) return true or false from the comparison.

Given that x = 5, the table below explains comparison:

Loops

Booleans are extensively used in loops to determine conditions for looping.

The Boolean() Function

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

Example : Boolean(10 > 9)

Everything With a "Value" is True

Note

  • In JavaScript, both an empty array [ ] and an empty object { } are truthy because they are objects.

  • All objects in JavaScript evaluate to true in a boolean context, regardless of their content.

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

let x = "";
Boolean(x);

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

let x = -0;
Boolean(x);

The Boolean value of undefined is false:

let x;
Boolean(x);

The Boolean value of null is false:

let x = null;
Boolean(x);

JavaScript Booleans as 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);

In JavaScript, truthy **and **falsy values are concepts related to boolean evaluation. Every value in JavaScript has an inherent boolean "truthiness" or "falsiness," which means they can be implicitly evaluated to true or false in boolean contexts, such as in conditional statements or logical operations.

What Are Truthy Values?

Truthy values are values that are evaluated to be true when used in a Boolean context. Simply put, any value that is not explicitly falsy is considered truthy

  • Non-zero numbers: 42, -1, 3.14
  • Non-empty strings: "hello", "0", " "
  • Objects and arrays: {}, []
  • Functions: function() {}
  • Dates: new Date()
  • Symbols: Symbol()
  • BigInt values other than 0n: 10n

What Are Falsy Values?

Falsy values are values that evaluate to false when used in a Boolean. JavaScript has a fixed list of falsy values

  • false
  • 0 (and -0)
  • 0n (BigInt zero)
  • "" (empty string)
  • null
  • undefined
  • NaN
  • document.all (used for backward compatibility

Warning

Do not create Boolean objects.

The new keyword complicates the code and slows down execution speed.

Boolean objects can produce unexpected results:

Reference :

Top comments (1)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

...the double negation operator

There is no such operator, you are using the negation operator twice.