Boolean In Javascript

Aug 9, 2024

boolean in Javascript

Declaring and Assigning Boolean Values

To declare and assign a Boolean variable in JavaScript, you can use the following syntax:

let isTrue = true;
let isFalse = false;

Here, the variables isTrue and isFalse are assigned the Boolean values true and false, respectively. It's important to note that you should never explicitly specify a Boolean value as a string or as an object. Strings are not the correct data type for Boolean expressions, while creating Booleans as objects results in unnecessary overhead and complicates code.

let example2a = "true"; // Incorrect: not a Boolean
let example2b = new Boolean(true); // Incorrect: overcomplicated

Comparison Operators and Boolean Results

JavaScript provides several comparison operators that return Boolean values when used to compare two values. These operators include:

  • Equality (==): Checks if two values are equal and returns true if they are, false otherwise.

  • Inequality (!=): Checks if two values are not equal and returns true if they are not, false otherwise.

  • Strict Equality (===): Checks if two values are equal in both value and data type and returns true if they are, false otherwise.

  • Strict Inequality (!==): Checks if two values are not equal in either value or data type and returns true if they are not, false otherwise.

  • Greater Than (>): Checks if the value on the left is greater than the value on the right and returns true if it is, false otherwise.

  • Less Than (<): Checks if the value on the left is less than the value on the right and returns true if it is, false otherwise.

  • Greater Than or Equal To (>=): Checks if the value on the left is greater than or equal to the value on the right and returns true if it is, false otherwise.

  • Less Than or Equal To (<=): Checks if the value on the left is less than or equal to the value on the right and returns true if it is, false otherwise.

Here's an example of using comparison operators in JavaScript:

console.log(5 > 3); // Output: true
console.log(10 <= 10); // Output: true
console.log("apple" === "banana"); // Output: false

Logical Operators and Boolean Operations

JavaScript provides logical operators that allow you to perform logical operations on Boolean values or expressions. The three main logical operators are:

  • Logical AND (&&): Returns true if both operands are true, and false otherwise.

  • Logical OR (||): Returns true if at least one of the operands is true, and false if both operands are false.

  • Logical NOT (!): Negates the Boolean value of an operand. If the operand is true, it returns false, and if the operand is false, it returns true.

These logical operators can be used to combine Boolean values, create complex conditions, and make decisions based on multiple criteria.

console.log(true && true); // Output: true
console.log(true && false); // Output: false
console.log(false || true); // Output: true
console.log(!false); // Output: true

Truthy and Falsy Values

In JavaScript, certain values are considered "truthy" or "falsy" when used in a Boolean context. Truthy values are treated astruewhen evaluated in a Boolean context, while falsy values are treated asfalse.The following values are considered falsy in JavaScript:

  • false

  • 0 (zero)

  • 0n (BigInt zero)

  • '', "", or ` ` (empty strings)

  • null

  • undefined

  • NaN (Not a Number)

All other values, including non-zero numbers, non-empty strings, objects, and arrays, are considered truthy.

if ("") {
  console.log("This will not be logged because an empty string is falsy");
}

if ([]) {
  console.log("This will be logged because an empty array is truthy");
}

It's important to note that when using non-strict comparison operators (== or !=), JavaScript will perform type coercion, which can lead to unexpected results. It's generally recommended to use strict comparison operators (=== or !==) to avoid such issues.

Managing Boolean Values

Since JavaScript Booleans can be explicit or implicit, you should always be aware of whether you are using strict JavaScript operators to evaluate Boolean expressions. "Truthy" and "falsy" expressions only evaluate as equal to explicit Booleans if you are not using strict comparisons.

// Evaluates to true because a non-empty string is "truthy"
let example6a = ("1" == true);

// Evaluates to false with strict comparison
let example6b = ("1" === true);

The flexibility of using non-strict comparisons with "truthy" and "falsy" expressions can be both helpful and harmful. It may make programming easier, but maintenance may be tougher in the long term because it could be hard to tell which values are being compared.

Boolean Primitives and Boolean Objects

In JavaScript, you can explicitly convert non-Boolean values to Boolean values using the Boolean() function or the double NOT operator (!!). However, it's important to avoid using the Boolean() constructor with new.

const good = Boolean(expression);
const good2 = !!expression;

const bad = new Boolean(expression); // Don't use this!

This is because all objects, including a Boolean object whose wrapped value is false, are truthy and evaluate to true in places such as conditional statements.

if (new Boolean(true)) {
  console.log("This log is printed.");
}

if (new Boolean(false)) {
  console.log("This log is ALSO printed.");
}

Conclusion

Booleans are a fundamental concept in JavaScript and serve as the building blocks for decision-making and logical operations in programming. By understanding the syntax of Booleans and utilizing logical operators effectively, you can create robust and flexible JavaScript code.Remember the key points about Booleans in JavaScript:

  • Booleans can have either true or false values

  • Use comparison operators to get Boolean results

  • Utilize logical operators like &&, ||, and ! for complex conditions

  • Be aware of truthy and falsy values and use strict comparisons

  • Avoid creating Booleans as objects and use primitives instead

Practice using Booleans in various scenarios to master their implementation and take full advantage of their power in your JavaScript projects.

Happy coding!