HOME C C++ PYTHON JAVA HTML CSS JAVASCRIPT BOOTSTRAP JQUERY REACT PHP SQL AJAX JSON DATA SCIENCE AI

JavaScript Booleans

In JavaScript, a Boolean is a data type that represents one of two values: true or false.
Booleans are commonly used in conditional statements and logical operations to control the flow of code execution.

Here's an overview of how Booleans work in JavaScript:

  • Boolean Literals:
  • Boolean Values:
  • Boolean Operators:
  • Boolean Functions:

Boolean Literals:

The two Boolean literals in JavaScript are true and false.

They are case-sensitive and must be written in lowercase.

Example

 let isTrue = true;
let isFalse = false;
 

Boolean Values:

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

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

Example

 let falsyValue = false;
if (falsyValue) {
    console.log('This will not be executed.');
} else {
    console.log('Falsy values evaluate to false in a Boolean context.');
}
 

Boolean Operators:

JavaScript provides several operators for working with Boolean values.

  • Logical AND (&&): Returns true if both operands are true.
  • Logical OR (||): Returns true if at least one operand is true.
  • Logical NOT (!): Returns true if the operand is false, and false if the operand is true.

Example

let a = true;
let b = false;
console.log(a && b); // Output: false
console.log(a || b); // Output: true
console.log(!a);     // Output: false

Boolean Functions:

Functions can return Boolean values based on some condition.