Logical Operators(JavaScript)

Japanese version.

These operators are used to manipulate boolean values.

Explanation

&& AND

The logical AND (&&) operator in JavaScript returns true if both the left-hand and right-hand operands are true, otherwise it returns false.

For example, you can use it like this:

let a = 5;
let b = 10;

if (a > 0 && b > 0) {
  console.log("Both a and b are positive.");
}

In this example, since both the left-hand and right-hand operands are true, the result of the logical AND operator will be true, and the message "Both a and b are positive." will be logged to the console.

You can also use the logical AND operator to combine multiple conditions. Here's an example:

let age = 25;
let isStudent = true;

if (age >= 18 && age <= 30 && isStudent) {
  console.log("You are eligible for a student discount.");
}

In this example, since both the left-hand and right-hand operands are true, the result of the logical AND operator will be true, and the message "Both a and b are positive." will be logged to the console.

This operator also has short-circuit evaluation property, meaning that if the left-hand operand is false, the right-hand operand is not evaluated, and the result of the operator will be false.

For example, if a function with a Boolean return value is on the right-hand side, the function on the right-hand side will be executed if the left-hand side is true.

let a = 0;
let b = true;

if (b && f() ) {
  console.log("True");
}

console.log(a); // 1

function f(){
  a = a + 1;
  return true;
}

In this example, the function f() is executed and the variable a is updated to 1 before being displayed.

However, if the left-hand side is false, the function on the right-hand side is not executed because the result is determined to be false, whatever the right-hand side is.

let a = 0;
let b = false;

if (b && f() ) {
  console.log("True");
}

console.log(a); // 0

function f(){
  a = a + 1;
  return true;
}

In this example, the variable a is displayed as 0 without the function f() being executed.

This is equivalent to nesting ifs as follows.

let a = 0;
let b = true;

if ( b ) {
  if( f() ){
    console.log("True");
  }
}

console.log(a); // 1

function f(){
  a = a + 1;
  return true;
}

Nesting if can make a program difficult to read, and short-circuit behaviour is sometimes used to avoid this. However, this makes it harder to decipher, so nesting if is a clearer expression of intent. Nesting if is basically recommended, as it can lead to misreading and unintended behaviour, especially when more than one person is expected to see the programme.

|| OR

The logical OR (||) operator in JavaScript returns true if either the left-hand or right-hand operand is true, and false otherwise. Like the logical AND operator, it also has short-circuit evaluation property, meaning that if the left-hand operand is true, the right-hand operand is not evaluated, and the result of the operator will be true.

Here's an example of using the logical OR operator:

let a = -5;
let b = 10;

if (a < 0 || b < 0) {
  console.log("At least one of a and b is negative.");
}

In this example, since either a or b is negative, the result of the logical OR operator will be true, and the message "At least one of a and b is negative." will be logged to the console.

You can also use the logical OR operator to combine multiple conditions. Here's an example:

let age = 65;
let isStudent = false;

if (age >= 65 || isStudent) {
  console.log("You are eligible for a discount.");
}

In this example, if age is greater than or equal to 65 or isStudent is true, the result of the logical OR operator will be true, and the message "You are eligible for a discount." will be logged to the console.

! NOT

The logical NOT (!) operator in JavaScript is a unary operator that returns false if its single operand can be converted to true; otherwise, returns true. This operator represents the logical negation of its operand.

Here's an example of using the logical NOT operator:

let isRaining = false;

if (!isRaining) {
  console.log("It's not raining.");
}

In this example, since isRaining is false, the result of the logical NOT operator will be true, and the message "It's not raining." will be logged to the console.

Additionally, the logical NOT operator can be used to convert non-Boolean values to Boolean. If the operand is null, undefined, 0, NaN, or an empty string (''), the result of the logical NOT operator will be true. Otherwise, the result of the logical NOT operator will be false.

Here's an example of using the logical NOT operator to convert a value to a Boolean:

let num = 0;
if (!num) {
  console.log("The value of num is false.");
}

In this example, since the value of num is 0, which is false, the result of the logical NOT operator will be true, and the message "The value of num is false." will be logged to the console.

---

Links

JavaScript Articles