


Comparison of js operator precedence and analysis of js logical operators
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
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
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
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
- Auto-increment operator and auto-decrement operator
a. Preposition type: The auto-increment operator is placed before the operand and adds 1 before assigning the value
前置自增运算符 - ++变量名
后置自增运算符 - 变量名++
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
Comparison operators
一.大于与小于比较运算符
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('a' < 'b');//true console.log('a' > 'A');//true console.log('abc' > 'cba');//false console.log('abc' > 'acd');//false console.log('我' > '你');//true
二.相等与不等比较运算符
*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('a' == 'a');// true console.log('a' == 'b');// false console.log('100' == 100);// true
三.全等与不全等运算符 全等与不全等运算符 不仅比较值 还比较类型
console.log(10 === 10);// true console.log('10' === 10);// false console.log('10' == 10);// true
- Function
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('卧龙学苑')));// true var result = 100/0; console.log(result);// Infinity //isFinite()函数 console.log(isFinite(result));// false
- Logical AND operator
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('卧龙学苑' && '卧龙学苑');// 卧龙学苑 console.log('卧龙学苑' && '');// '' console.log('' && '卧龙学苑');// '' console.log('' && '');// '' console.log('卧龙学苑' && 1);// 1 console.log(false && 0);// false
- Logical OR operator
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('卧龙学苑' || '卧龙学苑');// 卧龙学苑 console.log('卧龙学苑' || '');// 卧龙学苑 console.log('' || '卧龙学苑');// 卧龙学苑 console.log('' || '');// ''
- Assignment expansion operator
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
条件运算符
条件运算符 首先判断一个表达式是真是假 然后根据判断结果执行两个给指定指令中的一个
var result = 8 > 9 ? '对' : '错'; console.log(result); var score = 95; score > 90 ? console.log('优秀') : console.log('及格');
嵌套条件运算符
条件运算符中 每个表达式可以又是一个条件运算表达式 称为条件运算的嵌套
优点:扩展了条件运算符本身的计算能力
缺点:可读性比较差 性能随着嵌套的层级越多越差
建议:不要最多不要超过三层嵌套
var score = 55; var result = score > 90 ? '优秀' : (score > 80 ? '良好' : (score > 60 ? '及格' : '不及格')); console.log(result);
相关推荐:
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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 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

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 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 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

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

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 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
