search
HomeWeb Front-endJS TutorialBriefly describe the types of operators and usage rules in JavaScript

There are many operators in JavaScript, mainly divided into arithmetic operators, identical operators, comparison operators, string operators, logical operators, assignment operators, etc. They all have their own operation rules. , in this article I will introduce to you

There are many operators in JavaScript, mainly divided into arithmetic operators, identical operators, comparison operators, string operators, logical operators, assignment operators, etc. These operators have some own operation rules. Let’s introduce the operators in JavaScript to you.

 1. Types of JavaScript operators

 1. Arithmetic operators: +, -, *, /, %, -(unary negation), ++ , --

 2. Identity and identity operators: ==, ===, !==, !===

 3. Comparison operators: , =

4. String operators: , = , = , +

5. Logical operators: && , ||, !,

 6. Assignment operators: =, +=, *=, -=, /=

 2. Rules of JavaScript operators

 1. Arithmetic operator rules

"+": You can perform "addition" and "connection" operations; if one of the two operators is a string, JavaScript will use the other Convert to a string and then concatenate the 2 operands.

 “+”: If an operand is an object, JavaScript will convert the object into a number for addition or a string for concatenation;

 “-”“*”“/” If one of the two operands is a non-number, it is converted into a number to perform the mathematical operation.

 “/” In JavaScript, since all numbers are floating point numbers, the results of division are all floating point numbers, 5 / 2 = 2.5; the result of dividing by 0 is plus or minus infinity; 0/0 is NaN;

"%" modulo operator: Calculates the modulus of the first operand to the second operand, that is, when the first operand is divided by the second operand, the remainder is returned. If the operand is non-numeric, it is converted to a number.

 “-” Unary negation: Negate the operand. If the operand is not a number, convert it into a number.

 "++""--" Increment operator/decrement operator symbol: The operand must be a variable, an element of a tree group, or an attribute of an object. If the operand is not a number, it is converted to a number.

Note: If "++" is located before the operand, the operand is incremented first, and then the value after the operand is increased is calculated.

If "--" is located after the operand, first calculate the value before the operand is incremented, and then increment the operand.

For example: i = 1; //Assign the value 1 to i

j = ++i; //First increment i to 2, and then assign 2 to j, i The value is 2, and the value of j is also 2.

i = 1; //Assign i to value 1

j = i++; //Assign i to j first, and then increment i to 2. The value of i is 2. The value of j is 1.

The decrement operator "--" is the same as "++".

 2. Equality operator and identity operator

 (I) "==" "!==" equality operator and non-identity operator:

Comparison 2 operands, returns a non-Boolean value.

Comparing numerical values, strings, and Boolean values ​​all use quantitative values. Two variables are equal if and only if the values ​​stored in them are equal.

When comparing objects, arrays, and functions, references are used. Two variables are equal only when they refer to the same object. Two different arrays are completely different, even if they have exactly the same elements. For variables that store references to objects, arrays, and functions, they are equal only when they refer to the same object, array, or function.

Attention! Principles that should be followed:

When the types of the two operands are different: convert them into the same type,

1) A number and a character String, after converting the string into a number, compare.

 2) True is converted to 1 and false is converted to 0 for comparison.

 3) An object, array, function and a number or string, the object, array, or function is converted into a primitive type value and then compared. (Use valueOf first, and if that doesn’t work, use toString)

 4) Other types of combinations are not equal.

If the two operand types are the same, or after conversion to the same type:

1) Two strings: If the characters at the same position are equal, the two strings are the same.

 2) Two numbers: If the two numbers are the same, they are the same. Not the same if one is NaN, or if both are NaN.

 3) If both are true, or if both are false, it means the same.

 4) If two references refer to the same object, function, or array, they are equal. If they refer to different objects, functions, or arrays, they are not the same. Even if these two objects, functions, and arrays refer to Can be converted to an exact equivalent of the original value.

 5) Two nulls, or both are undefined, then they are equal.

 “!=” Non-Equality Operator: The result of the test is opposite to that of the equality operator.

 (II) “===” “!===” identity operator and non-identity operator symbol:

The identical operator follows the comparison rules of the equality operator, but it does not perform type conversion on the operands. When the types of the two operands are different, it returns false; only when the types of the two operands are the same, it follows the equality operation. Compare according to the comparison rules of symbols.

"!==" non-identical operator has the opposite result than the identical operator. Returns true if the types or values ​​of the two operands are different.

3. Comparison operators

These comparison operators are used to compare values ​​of different types, and the result returns a Boolean value.

 “”“=”

Note the rules: The operands for comparison can be of any type, but they can only be used in numerical and operation Execute on numbers. Operands that are not numbers and strings are converted to numbers or strings.

 1) If both operands are numbers, or both can be converted into numbers, the comparison will be based on the size of the numbers;

 2) If both operands are strings, Or they can be converted into strings, and they will be compared in alphabetical order;

 3) If the string encounters a number, the string will be converted into a number for comparison.

 4) If the operand cannot be converted into a number or a string, the result is false.

4. String operators

There are no dedicated string operators, but some operators behave differently when encountering string operands.

 (I) “+” connects two strings;

 1) When both operands are strings, connect them together;

 2) When When one of them is a number, convert the numbers into strings and connect them;

 (II) Comparison operators such as ">" confirm the order of two strings through comparison, and the comparison uses characters In order, smaller letters come before larger letters, and uppercase letters come before lowercase letters.

 (III) The action method of "+" depends on the calculation order,

For example: s = 1 + 2 +"var" then: the result 3var is returned; because 1+2 is calculated first, Then convert the result 3 into a string and connect it with "var";

For example: s = "var" + 1 + 2 Then: return the result var12; because the connection between var and 1 is calculated first, and then the result var1 Concatenate with 2 converted to string.

 5. Logical operators

Used to perform Boolean operations, often used together with comparison operators to express complex comparison operations.

 “&&” logical AND operation, “||” logical OR operator, “!” logical NOT operator

  (I) “&&” when both operands are Boolean values When, the logical AND operation is performed on them, that is: if and only if both Boolean values ​​are true, the result is true, otherwise it is returned false.

Note: Actual performance

"&&" will detect the Boolean value of the first expression operand. If the first operand expression returns false, the first operand on the left will be returned. The value of the expression: false; otherwise, the second operand expression on the right will continue to be detected, and then the value of the second operand expression will be returned;

For example: if (a = b) stop( ); Equivalent to (a = b) && stop();

This method is deprecated because the code on the right side of the operator is not guaranteed to be executed,

For example: if (( a
It will be simpler and safer to regard "&&" as a Boolean algebra operator.

 (II) "||" When both operands are Boolean values, logical OR performs an OR operation on them, that is: when one of the two Boolean values ​​is true, the result is true. , otherwise return false.

Note: Actual performance

"||" will detect the Boolean value of the first expression operand. If the first operand expression returns true, the first operation on the left will be returned. The value of the number expression: true; otherwise, the second operand expression on the right will continue to be detected, and then the value of the second operand expression will be returned;

The use of this method is also deprecated, because the operation The code on the right side of the symbol is not guaranteed to be executed.

It is generally opposed to using expressions with other functions (assignment, function call, increment and decrement) on the right side of ||;

Replace "| |" is considered as a Boolean algebra operator, which is simpler and safer.

 (III) "!" logical negation is a unary operator, placed before the operand, and its purpose is to negate the operand.

 6. Assignment operator

 (I) "=" is the assignment operator; it always expects the operand on the left to be a variable, an element of an array, or an attribute of an object;

The right-hand side is expected to be an arbitrary value of any type;

From right to left associativity, if there are multiple assignment operators in an expression, calculation starts from the rightmost one.

Note: Each assignment expression has a value, which is the value on the right side of the operator;

 (II) You can use the assignment operation with operation

"+=" Add the value on the left After the value on the right, assign it to the value on the left. “-=”, “/=” and “*=" have the same method;

7. Other operators

The “?:” conditional operator is the only ternary operator;

The Boolean result of an expression? Expression 1 (any value of any type): Expression 2 (any value of any type);

Based on the Boolean result of the first operand, If it is true, the second operand expression is executed and the value of the second operand expression is returned; if the Boolean result of the first operand is false, the third operand expression is executed and the value of the second operand expression is returned. The value of the three operand expression.

 3. Notes on JavaScript operators

 1. Pay attention to the data type passed to the operator and the data type returned! Different operators expect it An operand expression evaluates to a result that conforms to a data type.

For example: String multiplication cannot be performed, "a" * "b" is illegal, but when possible, JavaScript will convert the expression into the correct type, so , the expression "3" * "5" is legal. JavaScript converts the string into a number and performs the operation. The result is the number 15, not the string "15".

2. + has different performances depending on the operands:

String + string = string (connected); "a" + "b" = "ab" "5" + "6" = "11"

 String + number = (string converted to number) string (concatenated); "a" + 5 = "a5" 5 is converted to character String "1" + 0 = "10"

 Number + Number = Number (Add) 5 + 5 = 10.

 3. Pay attention to the associativity of operators. Some operators are associative from left to right; some are associative from right to left.

 For example: w = a + b + c is equivalent to w = (a + b) + c;

 w = ---b is equivalent to w = - ( - ( -b ) ); w = a = b = c is equivalent to w= ( a = ( b = c ))

The associativity of unary operators, assignment operators, and ternary operators is from right to left;

The JavaScript operators are introduced to you here. I hope it can be helpful to you in your daily life.

The above is the detailed content of Briefly describe the types of operators and usage rules 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
es6数组怎么去掉重复并且重新排序es6数组怎么去掉重复并且重新排序May 05, 2022 pm 07:08 PM

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

JavaScript的Symbol类型、隐藏属性及全局注册表详解JavaScript的Symbol类型、隐藏属性及全局注册表详解Jun 02, 2022 am 11:50 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

原来利用纯CSS也能实现文字轮播与图片轮播!原来利用纯CSS也能实现文字轮播与图片轮播!Jun 10, 2022 pm 01:00 PM

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

JavaScript对象的构造函数和new操作符(实例详解)JavaScript对象的构造函数和new操作符(实例详解)May 10, 2022 pm 06:16 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

JavaScript面向对象详细解析之属性描述符JavaScript面向对象详细解析之属性描述符May 27, 2022 pm 05:29 PM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

javascript怎么移除元素点击事件javascript怎么移除元素点击事件Apr 11, 2022 pm 04:51 PM

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

整理总结JavaScript常见的BOM操作整理总结JavaScript常见的BOM操作Jun 01, 2022 am 11:43 AM

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。

foreach是es6里的吗foreach是es6里的吗May 05, 2022 pm 05:59 PM

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

See all articles

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)