1. Arithmetic operator +-* /% (remainder sign)
The computer performs automatic data type conversion: there is no way to perform operations between different data types, convert the data to the same data type, and then perform calculations.
- One of the operands is a string, and the operator is a + sign. When other data types are converted into strings, the two strings will be spliced together.
alert('hello' + 100);//hello100
alert('hello' + true);//hellotrue
alert('hello' + NaN);//helloNaN
alert('hello' + undefined);//helloundefined
alert('hello' + 'de');//hellode
- Any data and string do operations other than +, then the string must be converted into a number before performing the operation. (100-"20" results in 80)
-
If the string is a string composed of pure numeric characters, convert it to the corresponding number;
-
If the string contains characters other than numbers, it is converted into NaN, NaN and any data operation are NaN;
alert(100 - '20');//相当于100-20 结果为80
alert(100 -"20a");//'20a'转换成数字为NaN,所以结果为NaN
- When performing arithmetic operations on data other than character strings, first convert them to numbers, and then perform operations
alert(10 + true);//true=>1
alert(10 +false); //false=>0
alert(100 - NaN); //结果NaN
alert(100 -undefined);//结果NaN
alert(100 -null); //结果100 null=>0
note:
<1>The computer will not perform decimal operations.
<2>The divisor can be 0 in js
alert(0.8-0.1)
alert(10 / 0);//Infinity
alert(-10 / 0);//-Infinity
Infinity是一个数字,可以跟数字运算 ,结果总是无穷大。
Second, the relational operator <> <= >= == != === !==
- The result of the judgment is absolutely a Boolean value
- The rules that different data types follow when comparing:
(1) If both operands are numeric values, then the numeric values are compared.
(2) If the two operators are both character strings, compare
the character code values corresponding to the two character strings. The ASCII code table is a table of codes corresponding to each character in the computer.
① If two single characters are compared, directly compare the ASCII code value.
②Compare bit by bit until the size is compared and the result is obtained.
"Abcd" <"ad": a is the same, b is 98, d is 100, so the result is true.
(3) If one of the two operands is a value, the other is converted to a value, and then the value is compared.
1 == true //true转成数字是1,false转成数字是0,比较结果是true
10 == "10" //结果是true
(4) NaN, == returns false, != returns true, and NaN is not equal to itself.
alert(10 == "10a" );//false
alert(10 != "10a");//true
alert("10a" == NaN);//false
-
=== Identities must be of the same number and data type before returning true
Note: Number(null) is 0; Number(undefined) is NaN; but null == undefined is true.
Third, the logical operator && ||!
-
AND operation:
&& (表达式1 && 表达式2 ) 只有当两个表达式都为true时,整个表达式才为真。 注意短路操作:只要左边一个为假,整个表达式就为假,不会再运行右边的代码。比如 10<5 && alert(num); 不会报未声明变量num的错误,因为右边不执行。
-
Or operation:
|| (表达式1 || 表达式2) 只有当两个表达式都为false时,整个表达式为假。 (同样表达式1为真的话就会不在运行表达式二,直接得出整个表达式为true)
-
Non-operation:
!表达式 将表达式的数据类型转换为布尔值(非0即真,非空即真),然后再取反。
Four, unary operator ++--
- a++:
Add 1 to the original variable, first take the value of a as the value of a++ expression, and then add one to a
- ++a:
To add 1 to the original variable, first add one to a, and then use the value of a as the expression of ++a.
var a = 5;
alert(a++);//5
alert(++a);//7 上一语句先将a的值作为a++的值输出,再对a加一变成6,此句将a加一变成7,再讲a得值7作为++a的值输出。
- a- - 、 - -a
Decrement the original variable by 1
Five, assignment operator basic = compound += -= *
“=” 将等号右边的值赋值给等号左边的变量:
var num = 10 + 20;
alert(num); //结果是30
“+=” : num = num + 9 => num += 9