Home > Web Front-end > JS Tutorial > body text

what are logical operators in js

下次还敢
Release: 2024-05-09 00:36:17
Original
573 people have browsed it

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>
    Copy after login

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>
    Copy after login

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>
    Copy after login

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!

Related labels:
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