Home  >  Article  >  Web Front-end  >  What is the symbol in javascript?

What is the symbol in javascript?

青灯夜游
青灯夜游Original
2022-01-27 15:40:3810001browse

In JavaScript, it is the "&&" symbol. "&&" is a logical operator, which means "logical AND", which is equivalent to "and" in life; when using this operation operator, it will return true only when the operands on both sides are true, otherwise it will return false.

What is the symbol in javascript?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

In JavaScript, it is the "&&" symbol.

"&&" is a logical operator, meaning "logical AND", which is equivalent to the "AND" in life, which is the "logical AND" when two conditions are established at the same time. The result of the operation is "true".

Logical AND operation "&&"

Logical AND operation (&&) is AND Boolean operations. Returns true only if both operands are true, otherwise returns false. The detailed description is shown in the table.

Logical AND operations
          The first operand           The second operand           Operation result
          true           true           true
          true           false           false
          false           true           false
          false           false           false

#Logical AND is a kind of short-circuit logic. If the expression on the left is false, the result will be short-circuited and returned directly, and the expression on the right will no longer be evaluated. The operation logic is as follows:

  • Step 1: Calculate the value of the first operand (expression on the left).

  • Step 2: Detect the value of the first operand. If the value of the expression on the left is convertible to false (such as null, undefined, NaN, 0, "", false), then the operation will end and the value of the first operand will be returned directly.

  • Step 3: If the first operand can be converted to true, evaluate the second operand (the expression on the right).

  • Step 4: Return the value of the second operand.

Explanation: The logical AND operation is a short-circuit operation, that is, if the first operand can determine the result, then the second operand will not be evaluated. For logical AND operations, if the first operand is false, no matter what the value of the second operand is, the result cannot be true, which is equivalent to short-circuiting the right side.

Example:

The following code uses logical AND operations to detect variables and initialize them.

var user;  //定义变量
(! user && console.log("没有赋值"));  //返回提示信息“没有赋值”

Equivalent to:

var user;  //定义变量
if (! user){  //条件判断
    console.log("变量没有赋值");
}

If the value of variable user is 0 or a false value such as an empty string is converted to a Boolean value, it will be false. Then when the variable is assigned a value, it will still prompt The variable has no value assigned. Therefore, when designing, you must ensure that the return value of the expression on the left side of the logical AND is a predictable value.

var user = 0;  //定义并初始化变量
(! user && console.log("变量没有赋值"));  //返回提示信息“变量没有赋值”

The expression on the right side should not contain valid operations such as assignment, increment, decrement, and function call, because when the expression on the left side is false, the expression on the right side is skipped directly, which will affect the subsequent operations. bring potential impact.

[Related recommendations: javascript learning tutorial]

The above is the detailed content of What is the symbol in javascript?. 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