Home  >  Article  >  Web Front-end  >  what are logical operators in js

what are logical operators in js

下次还敢
下次还敢Original
2024-05-09 00:36:17550browse

Logical operators in JS

Logical operators are used to combine two or more Boolean values ​​(true/false) to generate a new Boolean value. In JavaScript, there are the following three logical operators:

  1. and (&&)
  2. or (||)
  3. Not (!)

1. And (&&)

  • ## means: It is true only if it is true at the same time
  • Usage: Connect multiple Boolean values. If all Boolean values ​​are true, the result is true; otherwise, it is false.
  • Example:

    <code class="js">const isLoggedIn = true;
    const hasAccess = true;
    
    const canAccess = isLoggedIn && hasAccess; // true</code>

2. Or (||)

  • means: It is true only if at least one is true
  • Usage: Connect multiple Boolean values. If any Boolean value is true, then the result is true; otherwise, it is false.
  • Example:

    <code class="js">const isMember = false;
    const isFriend = true;
    
    const canJoin = isMember || isFriend; // true</code>

3. Not(!)

  • means: Negate, change true to false, change false to true
  • Usage: Operate on a Boolean value and negate its value.
  • Example:

    <code class="js">const isNight = false;
    
    const isDay = !isNight; // true</code>

Usage scenarios

Logical operators in JavaScript Widely used in:

    Conditional statements (if-else)
  • Loop control
  • Data validation
  • Construct complex logical expressions

The above is the detailed content of what are logical operators in js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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