Home > Web Front-end > JS Tutorial > How Do Logical Operators (&&, ||, and !) Work in JavaScript?

How Do Logical Operators (&&, ||, and !) Work in JavaScript?

Barbara Streisand
Release: 2024-11-10 08:49:02
Original
208 people have browsed it

How Do Logical Operators (&&, ||, and !) Work in JavaScript?

Logical Operators in JavaScript: Unraveling the Mysteries of &&, ||, and !

Understanding logical operators can be a challenge, especially when dealing with both boolean and other data types. This article aims to provide a comprehensive explanation of the behavior of &&, ||, and ! in JavaScript, offering a clear framework for their usage.

Boolean Operators: && and ||

These operators perform boolean operations, returning a boolean result. && (logical AND) returns true if both operands are true, and false otherwise. || (logical OR) returns true if at least one operand is true, and false only if both operands are false.

For example:

console.log(true && true); // true
console.log(true || false); // true
Copy after login

Type Coercion in Logical Operations

JavaScript performs type coercion when evaluating logical operators with operands of different data types. Falsy values are coerced to false, while truthy values remain true.

The following table summarizes type coercion in logical operations:

Operand 1 Operand 2 Result
True Any True
False True False
False False False
True Falsy True
Falsy True False
Falsy Falsy False

Short-Circuit Evaluation

Logical operators follow the principle of short-circuit evaluation. If a true value is evaluated for &&, there is no need to evaluate the second operand, as the result is already determined. Similarly, if a false value is evaluated for ||, there is no further evaluation required.

For example:

if (user.firstName && user.lastName) {
  // Username is valid, proceed
}
Copy after login

Negation Operator: !

The negation operator ! (logical NOT) converts a value to its boolean opposite. True values are converted to false, while false values are converted to true.

For example:

console.log(!true); // false
console.log(!false); // true
Copy after login

In addition, the following values are considered falsy in JavaScript:

  • null
  • undefined
  • false
  • 0
  • -0
  • NaN
  • 0n
  • ""
  • document.all

By understanding the behavior of these logical operators and considering type coercion, you can effectively implement conditional logic and data manipulation tasks in your JavaScript code.

The above is the detailed content of How Do Logical Operators (&&, ||, and !) Work in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template