Home > Web Front-end > JS Tutorial > Summary of various operators in JavaScript

Summary of various operators in JavaScript

怪我咯
Release: 2017-07-04 15:23:25
Original
1355 people have browsed it

The operators we are talking about here include arithmetic operators and logical operators, including Boolean and assignment, etc., we have carried out the JavaScript for everyone For a summary of the use of various operators, friends in need can refer to

Unary Operator
An operator that can only operate on one value is called a unary operator.
The unary operator is the simplest operator in ECMAScript.

1. Increment and decrement operators
The increment and decrement operators are directly borrowed from C, and each has two versions: prefix type and postposition type. Gu Mingsiyi, the prefixed type should be placed before the variable to be operated on, and the postfixed type should be placed after the variable to be operated on.
Pre-type:

var num1 = 1;
var num2 = 2;
var num3 = ++num1 + num2;//4
Copy after login

Post-type:

var num1 = 1;
var num2 = 2;
var num3 = num1++ + num2;//3
Copy after login

The above two pieces of code have different results. The reason is that there is a difference between post-increment and decrement and pre-increment and decrement. A very important difference is that post-increment-decrement operations are performed after the statements containing them are evaluated.

Applicable scope:
Preincrement and decrement and postincrement and decrement. All these four operators are applicable to any value. When applied to different values, this operator will convert the value like the Number() conversion function, and then add or subtract 1 after conversion.

2. Unary addition and subtraction operators
The unary addition and subtraction operators are mainly used for basic arithmetic operations and can also be used to convert data types (This operator will convert this value like the Number() conversion function).

Boolean operators
There are three Boolean operators: NOT (NOT), AND (AND), or (OR).

1. Logical NOT
The logical NOT operator is represented by an exclamation point (!) and can be applied to any value in ECMAScript. Regardless of the data type of the value, this operator returns a Boolean value.

Using two logical NOT operators at the same time will actually simulate the behavior of the Boolean() transformation function

2. Logical AND
Logical AND operator Represented by two ampersands (&&), it has two operands and can be applied to any type of operand. Logical AND is a short-circuit operation, that is, if the first operand evaluates to false, then the second operand will not be evaluated.
When both values ​​are true, the result is true. When two values ​​are one true and one false, the result is false. When both values ​​are false, false is returned.
When one of the values ​​is not a Boolean value: follow the following rules

If the first operand is false, return the first one;

When the first operand is true, return the second.
If the first operand is an object, return the second operand

var a = {b:1};
a && 'ss'//"ss"
Copy after login

If the second operand is an object, then only if the evaluation result of the first operand is true The object will only be returned if

'ss' && a//Object {b: 1}
Copy after login

If both operands are objects, the second operand will be returned

var c = {d:2};
c && a//Object {b: 1}
Copy after login

(1) If one operand is null, null will be returned
(2) If one of the operands is NaN, then NaN is returned
(3) If one of the operands is undefined, then undefined is returned

3. Logical OR
Similar to the logical AND operator, the logical OR operator is also a short-circuit operator. That is, if the first operand evaluates to true, the second operand will not be evaluated.

(1) If the first operand is true, return the first
(2) If the first operand is false, return the second
Multiplicative operator
ECMAScript defines 3 multiplicative operators: multiplication, division and modulo

Infinity*0//NaN
0/0//NaN
Infinity/Infinity//NaN
Copy after login

Additive operators
1. Addition (convert to string)
Both operators are numerical values
Perform regular addition calculations.

Infinity + -Infinity//NaN
Copy after login

If one of the operands is a string

If both operators are strings, concatenate the second operator with the first operator
If If only one of the operators is a string, convert the other operand to a string, and then concatenate the two strings.
If the operand is an object, numeric value or Boolean value, call their toString() method to obtain the corresponding string value, and then apply the previous rules about strings. For null and undefined, call the String() function and obtain the strings "undefined" and "null" respectively.

2 + '' //"2"
Copy after login

2. Subtraction (convert numeric value)
If both operands are numeric values
Perform the regular arithmetic subtraction operation and return the result, if there is one operand is NaN, the result is NaN

Infinity - Infinity//NaN
Copy after login

If one of the operands is not a numeric value

If one of the operands is a string, Boolean value, null or undefined, first call Number( ) function converts it to a numeric value, and then performs the subtraction calculation according to the previous rules. If the result of the conversion is NaN, the result of the subtraction is NaN.
If one of the operands is an object, call the object's valueOf() method to obtain the value representing the object. If the resulting value is NaN, the result of the subtraction is NaN. If the object does not have a valueOf() method, its toString() method is called and the resulting string is converted into a numerical value.

5 - true//4
Copy after login

关系操作符
如果两个操作数都是数值,则执行数值比较
如果两个操作数都是字符串,则比较两个字符串对应的字符编码值
如果一个操作数是数值,则将另一个操作数转换为数值,然后执行数值比较

var result = &#39;23&#39; < &#39;3&#39;//true
var result = &#39;23&#39; < 3//false
Copy after login

相等操作符
1.相等和不相等
先转换再比较

(1)如果有一个操作数是布尔值,则在比较相等性之前,先将其转换为数值
(2)如果有一个操作数是字符串,另一个操作数是数值,先将其转换为数值
(3)如果有一个操作数是对象,另一个不是,则调用对象的valueOf()方法,用得到的基本类型值按前面的基本规则进行比较
null和undefined是相等的
要比较相等性之前不能将null和undefined转换为任何其他值
如果两个操作数都是NaN,相等操作符也返回false,按规则,NaN不等于NaN

2.全等和不全等
仅比较而不转换

"55" !== 55 //true
Copy after login

条件操作符

variable = boolean_expression ? true_value : false_value
Copy after login

本质上,这段代码的含义就是基于对boolean_expression求值的结果,决定给变量variable赋什么值。如果求值结果为true,则给变量赋true_value;如果求值结果为false,则给变量variable赋false_value值。

赋值操作符
简单的赋值操作符由等号表示,其作用就是把右侧的值赋给左侧的变量。

逗号操作符
逗号操作符多用于声明多个变量;但除此之外,逗号操作符还用来赋值。在用于赋值时,逗号操作符总会返回表达式中的最后一项。

The above is the detailed content of Summary of various operators in JavaScript. 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