Table of Contents
Operator Overview
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

Aug 08, 2018 pm 05:19 PM
javascript

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

JavaScript and WebSocket: Building an efficient real-time image processing system JavaScript and WebSocket: Building an efficient real-time image processing system Dec 17, 2023 am 08:41 AM

JavaScript is a programming language widely used in web development, while WebSocket is a network protocol used for real-time communication. Combining the powerful functions of the two, we can create an efficient real-time image processing system. This article will introduce how to implement this system using JavaScript and WebSocket, and provide specific code examples. First, we need to clarify the requirements and goals of the real-time image processing system. Suppose we have a camera device that can collect real-time image data

See all articles