Home > Web Front-end > JS Tutorial > Comparison of js operator precedence and analysis of js logical operators

Comparison of js operator precedence and analysis of js logical operators

php是最好的语言
Release: 2018-08-08 17:19:54
Original
2973 people have browsed it

This article provides a detailed explanation of different categories of operators, and then describes the priority of js operators. The priority refers to the calculated priority level of the operator. It will be introduced in detail below.

How to compare operators in js? Comparison operators in the JavaScript language are mainly used to compare whether the values ​​​​of two operands are equal or the size. The article has reference value and can be collected if needed.

Operator Overview

JavaScript provides a set of operators for operating data values, also known as operators
Operators can be operated according to different functions or the number of operating variables. Category

  • Arithmetic operators

1. If one or both of the operands are string types, JavaScript will automatically convert them to numeric values. When performing calculations
2. If one or two of the operands are of string type but the characters in them are not numbers, JavaScript will automatically convert the numeric value to a NaN result
3. Any operand is a NaN result. Both are NaN
4. Boolean values ​​false and true will be converted to 0 and 1 for calculation

  • Addition operator

##1 .If both operands are numeric values, addition calculation will be performed.

2. If strings are added, string concatenation operator (string string)
3. If Boolean type is added, Boolean will be added. Convert the type to a numeric value and then perform addition calculation

  • Subtraction operator

1. Before subtraction operation, convert other types to number type and then Calculation

2. If the string is subtracted - convert the string type to a numeric value and then perform the subtraction
3. If the Boolean type is subtracted - convert the Boolean type to a numeric value and then perform the subtraction Calculation

  • Remainder operator

The remainder operator is used to calculate the remainder after the two operands are divided

Result Whether a positive number or a negative number is related to whether the first operand is positive or negative (it has nothing to do with the second one)

    console.log(100 % 3);//1
    console.log(8 % 4);//0

    console.log(100 % -3);//1
    console.log(8 % 4);//0

    console.log(-100 % 3);//-1
    console.log(-8 % 4);//0

    console.log(-100 % -3);//-1
    //与减法的情况保持一致
    console.log('卧龙学苑' % 2);//NaN
Copy after login

  • Auto-increment operator and auto-decrement operator

The auto-increment operator is used for integer values ​​​​by 1 successively, which is divided into:

a. Preposition type: The auto-increment operator is placed before the operand and adds 1 before assigning the value

前置自增运算符 - ++变量名
Copy after login

b. Post-increment type: the auto-increment operator is located after the operand, first assigns the value and then adds 1

后置自增运算符 - 变量名++
Copy after login

The auto-decrement operator is used for integer values ​​-1 successively and is divided into:

a. Pre-position type: auto-decrement operator Before the operand, decrement it by 1 and then assign it
b. Postposition type: the auto-decrement operator is before the operand, assign it and then decrement it by 1

  • operator The precedence level Operators have calculated precedence levels
    1. Operators with higher precedence levels are calculated first
    2. Operators have the same level and are calculated from left to right
    3. The operator with the highest priority is "()"
    4. The expression that is evaluated first is wrapped with "()"

  •     console.log(100 + 200 - 150 * 3);// -150
        console.log(100 + 200 % 3);// 102
        console.log(2 * 200 % 3);// 1
    
        var num = 10;
        console.log(5 + ++num);// 16
    Copy after login
  • Comparison operators

Comparison operators in JavaScript language are mainly used to compare whether the values ​​​​of two operands are equal or the size

一.大于与小于比较运算符
Copy after login
    console.log(10 > 11);//false
    console.log(11 > 10);//true
    console.log(10 >= 10);//true
    // 2.boolean类型 - 将boolean类型转换为number类型
    console.log(true > false);//true
    console.log(true > 0);//true
/*
    3.string类型 - a.英文; b.中文
    * 英文或中文 - 将文本转换成Unicode码 - 对应具有数字值
    * 英文单词 - 从左至右的一次比较字母 Unicode 码的大小
*/
    console.log(&#39;a&#39; < &#39;b&#39;);//true
    console.log(&#39;a&#39; > &#39;A&#39;);//true
    console.log(&#39;abc&#39; > &#39;cba&#39;);//false
    console.log(&#39;abc&#39; > &#39;acd&#39;);//false

    console.log(&#39;我&#39; > &#39;你&#39;);//true
Copy after login
二.相等与不等比较运算符
Copy after login

Equality comparison operator

*The difference between assignment operator
*Assignment operator (=)
*Equality comparison operator (==)
Inequality comparison operator
*The symbol is "!="
*Not "<>"
Equality and inequality comparison operators only compare the values ​​of the operands and do not compare types

    // 1.number类型
    console.log(10 == 10);// true
    console.log(10 == 11);// false
    // 2.boolean类型
    console.log(true == true);// true
    console.log(true == false);// false

    console.log(true == 1);// true

    // 3.string类型
    console.log(&#39;a&#39; == &#39;a&#39;);// true
    console.log(&#39;a&#39; == &#39;b&#39;);// false
    
    console.log(&#39;100&#39; == 100);// true
Copy after login
三.全等与不全等运算符 
全等与不全等运算符 不仅比较值 还比较类型
Copy after login
    console.log(10 === 10);// true

    console.log(&#39;10&#39; === 10);// false
    console.log(&#39;10&#39; == 10);// true
Copy after login

  • Function

isNaN() function is used to determine whether its parameter is a NaN value (non-numeric value)

Function: determine whether the current value is NaN
true - means The current value is NaN (not a numeric value)
false - Indicates that the current value is not NaN (not a numeric value)
isFinite() function
Function - Determines whether the current value is infinity
false - Indicates The current value is infinity
true - indicates that the current value is a finite value

    console.log(isNaN(100));// false
    console.log(isNaN(Number(&#39;卧龙学苑&#39;)));// true

    var result = 100/0;
    console.log(result);// Infinity
    //isFinite()函数
    console.log(isFinite(result));// false
Copy after login

  • Logical AND operator

The basic usage of JavaScript operators is Used for calculations of Boolean types

When using the logical AND operator, if both operands are of Boolean type, the result returned will be true only when both operands are true, otherwise they will be false
1. The two operands of the logical AND operator are converted to Boolean type
2. When the left operand is true, the result is the value of the right operand
3. When the left operand is false, the result is the left operation The value of the number

    console.log(true && true);// true
    console.log(true && false);// false
    console.log(false && true);// false
    console.log(false && false);// false

    console.log(100 && 1);// 1
    console.log(1 && 0);// 0
    console.log(0 && 1);// 0
    console.log(0 && 0);// 0

    console.log(true && true);
    console.log(true && false);
    console.log(false && true);
    console.log(false && false);

    console.log(&#39;卧龙学苑&#39; && &#39;卧龙学苑&#39;);// 卧龙学苑
    console.log(&#39;卧龙学苑&#39; && &#39;&#39;);// &#39;&#39;
    console.log(&#39;&#39; && &#39;卧龙学苑&#39;);// &#39;&#39;
    console.log(&#39;&#39; && &#39;&#39;);// &#39;&#39;

    console.log(&#39;卧龙学苑&#39; && 1);// 1
    console.log(false && 0);// false
Copy after login

  • Logical OR operator

When using the logical OR operator, if both operands are of Boolean type, only if The result returned is true when one of the operation type numbers is true, otherwise it is false

    console.log(true || true);// true
    console.log(true || false);// true
    console.log(false || true);// true
    console.log(false || false);// false

    console.log(100 || 1);// 100
    console.log(1 || 0);// 1
    console.log(0 || 1);// 1
    console.log(0 || 0);// 0

    console.log(true || true);
    console.log(true || false);
    console.log(false || true);
    console.log(false || false);

    console.log(&#39;卧龙学苑&#39; || &#39;卧龙学苑&#39;);// 卧龙学苑
    console.log(&#39;卧龙学苑&#39; || &#39;&#39;);// 卧龙学苑
    console.log(&#39;&#39; || &#39;卧龙学苑&#39;);// 卧龙学苑
    console.log(&#39;&#39; || &#39;&#39;);// &#39;&#39;
Copy after login

  • Assignment expansion operator

JavaScript language The assignment operator in is used to assign values ​​to variables or properties.

The assignment expansion operator in JavaScript language is actually the abbreviation mode of assignment operator and arithmetic operator.
Assignment expansion operator in JavaScript language The execution efficiency is higher

    var b = 10, c = 20;

    var a = b + c;
    console.log(a);// 30

    // b = b + c;
    // 等价写法 - 赋值扩展运算符的性能要比赋值运算符更高
    b += c;
    console.log(b);// 30
Copy after login
  • 条件运算符

条件运算符 首先判断一个表达式是真是假 然后根据判断结果执行两个给指定指令中的一个

    var result = 8 > 9 ? &#39;对&#39; : &#39;错&#39;;
    console.log(result);

    var score = 95;
    score > 90 ? console.log(&#39;优秀&#39;) : console.log(&#39;及格&#39;);
Copy after login
  • 嵌套条件运算符

条件运算符中 每个表达式可以又是一个条件运算表达式 称为条件运算的嵌套
优点:扩展了条件运算符本身的计算能力
缺点:可读性比较差 性能随着嵌套的层级越多越差
建议:不要最多不要超过三层嵌套

    var score = 55;

    var result = score > 90 ? &#39;优秀&#39; : (score > 80 ? &#39;良好&#39; : (score > 60 ? &#39;及格&#39; : &#39;不及格&#39;));
    console.log(result);
Copy after login

相关推荐:

js比较和逻辑运算符的介绍

运算符优先级-PHP运算符优先级

The above is the detailed content of Comparison of js operator precedence and analysis of js logical operators. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template