Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the logical operators of javascript

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what are the logical operators of javascript". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

JavaScript logical operators are: 1, logical and "&&", only if both operands are true, return true;2, logical or "||", if both operands are true, or one of them is true, return true;3, logical not"! ", converts operand values to Boolean values and negates them.

Operating environment of this tutorial: Windows 7 system, Javascript version 1.8.5, Dell G3 computer.

Logical operators in javascript

Logical operators are usually used to combine multiple expressions. The result of logical operators is a Boolean value. There can only be two results, either true or false. The following table lists the logical operators supported in JavaScript:

Example operator names && logical and x && y means true if x and y are both true|| logical OR x|| Y means true if either x or y is true! Logical NOT! x means true if x is not true

logical and operation

The logical AND operation (&&) is an AND Boolean operation. Returns true only if both operands are true, otherwise returns false. Detailed descriptions are shown in the table.

logical AND operation first operand second operand operation result truefalsefal

Logic and is a short-circuit logic, if the left expression is false, then the direct short-circuit returns the result, no longer calculate the right expression. The logic is as follows:

Step 1: Compute the value of the first operand (the left-hand expression).

Step 2: Check the value of the first operand. If the value of the left-hand expression can be converted to false (null, undefined, NaN, 0,"", false), then the operation ends and the value of the first operand is returned directly.

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

Step 4: Return the value of the second operand.

Examples 1

The following code detects variables and initializes them using logical AND operations.

var user; //define variable (! user && console.log("No assignment"); //returns message "No assignment"

Equivalent to:

var user; //define variable if (! user){ //condition console.log("variable is not assigned");}

If the value of the variable user is 0 or a false value such as an empty string is converted to a Boolean value, it is false, so when the variable is assigned, it still indicates that the variable is not assigned. Therefore, you must design to ensure that the expression on the left side of the logical AND returns a predictable value.

var user = 0; //define and initialize variables (! user && console.log("variable not assigned"); //returns message "variable not assigned"

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

logical or operation

logical or operation|| is a Boolean OR operation. Returns true if both operands are true, or if one of them is true, otherwise returns false. The details are shown in the figure.

logical OR operation first operand second operand operation result truefalsetruefalsefalsef

Logical OR is also a short-circuit logic. If the expression on the left is true, the short-circuit returns the result directly, and the expression on the right is no longer operated. The logic is as follows:

Step 1: Compute the value of the first operand (the left-hand expression).

Step 2: Check the value of the first operand. If the value of the left-hand expression can be converted to true, the operation ends and returns the value of the first operand.

Step 3: If the first operand can be converted to false, calculate the value of the second operand (the expression on the right).

Step 4: Return the value of the second operand.

Example 2

For the following four expressions:

var n = 3;(n == 1) && console.log(1);(n == 2) && console.log(2);(n == 3) && console.log(3);( ! n ) && console.log("null");

They can be combined using logic or:

var n = 3;(n == 1) && console.log(1) || (n == 2) && console.log(2) || (n == 3) && console.log(3) || ( ! n ) && console.log("null");

Because the && operator has higher precedence than|| Operator, so you don't have to use parentheses for grouping. However, grouping with parentheses makes the code easier to read.

var n = 3;((n == 1) && console.log(1)) ||((n == 2) && console.log(2)) ||((n == 3) && console.log(3)) ||(( ! n ) && console.log("null")) ||

The logical AND and logical OR operators have two characteristics:

During logical operations, operands are temporarily converted to Boolean values, and then the next operation is determined according to the Boolean value, but the type of operand and the final return result are not affected.

Controlled by the first operand, the second operand may not be executed.

logical negation

Logic NOT operation! is Boolean negation (NOT). As a unary operator, placed directly before the operand, converts the operand value to a Boolean value, negates it, and returns it.

Example 3

Here are some examples of the logical NOT return values for special operands.

console.log( ! {} ); //if the operand is an object, return falseconsole.log ( ! 0 ); //if operand is 0, return trueconsole.log ( ! (n = 5); //returns falseconsole.log ( !) if the operand is any number other than zero. null ); //if the operand is null, return trueconsole.log ( ! NaN );//trueconsole.log ( !) if operand is NaN Infinity ); //if the operand is Infinity, return falseconsole.log ( ! ( - Infinity); //if operand is-Infinity, returns falseconsole.log ( ! undefined ); //returns true if the operand is undefined

Example 4

If you perform two logical NOT operations on an operand, you are converting the operand to a Boolean value.

console.log( ! 0 ); //return trueconsole.log ( ! ! 0 ); //returns false

The return value of logical AND and logical OR operations need not be a Boolean value, but the return value of logical NOT operations must be a Boolean value.

"What are the logical operators of javascript" is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report