JavaScript scripting language describes a set of operators for operating data values, including unary operators, Boolean operators, arithmetic operators, relational operators, ternary operators, bitwise operators and assignment operators.
An expression is a "phrase" in the JavaScript language, including a variable name (or literal) and an operator. The simplest expressions are literals or variable names. Of course there are also ways to combine simple expressions to create complex expressions.
1. Unary operator
(1) Increasing and decreasing--
var box1=100; ++box1;//相当于box=box+1 document.write("box1="+box1+"<br/>");//输出box1=101 var box2=100; --box2;//相当于box=box2-1 document.write("box2="+box2);//输出box2=99
The difference between front and rear
var box=100; var age=++box;//box先累加1为101,再赋值给age为101 var height=box++;//box先赋值给height为101,box再累加为102 document.write("age="+age+"<br/>");//输出age=101 document.write("height="+height+"<br/>");//输出height=101 document.write("box="+box);//输出box=102,原因是box经过了两次累加,所以是102
When there is no assignment operation, prefix and postfix are the same. However, during the assignment operation, if the increment or decrement operator is preceded, the preceding operator will be accumulated or decremented first and then assigned. If it is a postpositioned operator, the value will be assigned first and then accumulated or decremented.
(2) Addition and subtraction operators
is used for positive or negative operations, and also has the function of converting numeric strings into numerical forms.
var box = "20"; document.write(typeof box+"<br/>"); //输出string var age=-box; document.write(age+"<br/>");//输出-20 document.write(typeof age); //输出number
2. Arithmetic operators
There are five arithmetic operators specified in the JavaScript language, namely , -, *, / and % (remainder). If the value in an arithmetic operator is not a numeric value, then it will first be converted to a numeric value using the Number() conversion function (implicit conversion).
var box=100+"100"; document.write("box="+box+"<br/>");//输出100100 document.write(typeof box);//输出string
Why is this? When doing arithmetic operations in the JavaScript language, as long as one of them is a string, the result will be converted to a string. Equivalent to the string concatenation operator and can no longer be counted as an addition arithmetic operator.
var box="100"-10; document.write("box="+box+"<br/>");//输出90 var age=5/4; document.write("age="+age+"<br/>");//输出1.25 var height=("你的年龄是:"+(10+10));//括号强制优先级 document.write(height);//输出你的年龄是:20
Get the remainder
var box=10%3; document.write("box="+box);//输出1
3. Relational operators
The operators used for comparison are called relational operators: < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), == (relative), != (not equal), === (identical or congruent), !== (not congruent or not identical). Most relational operators return a Boolean value.
Like other operators, the following rules must be followed when relational operators operate on non-numeric values:
1Both operators are numerical values, then numerical comparison
2If both operands are strings, compare the character encoding values corresponding to the two strings
3If one of the two operands is a numerical value, convert the other into a numerical value and perform numerical comparison
4If one of the two operands is an object, call the value() method or toString() method first, and then compare the results.
var box1=3>2; document.write(box1+"<br/>");//输出true var box2="3">22; document.write(box2+"<br/>");//输出false var box3="3">"22"; document.write(box3+"<br/>");//输出true var box4="a">"B";//a为97,B为66 document.write(box4+"<br/>");//输出true var box5= "Blue"<"alpha";//Blue的第一个字母是B,alpha的第一个字母是a,a为97,B为66 document.write(box5) //输出true
In equality and inequality comparisons, if the operand is a non-numeric value, the following rules apply:
1If an operand is a Boolean value, it is converted to a numerical value before comparison, false is converted to 0, and true is converted to 1.
2If one operand is a string, it will be converted into a numerical value before comparison and then compared.
3If an operand is an object, call the value() method or toString() method first and then compare.
4 Without any conversion, null and undefined are equal
5If an operand is NaN, then == returns false, != returns true, and NaN is not equal to itself
6If both operands are objects, compare whether they are the same object. If they both point to the same object, return true, otherwise return false
7In the judgment of congruence and congruence, for example, if the values and types are equal, true will be returned, otherwise fasle will be returned.
var box1='2'==2; document.write(box1+"<br/>");//输出true,比较的只是数值 var box2={}=={}; document.write(box2+"<br/>");//输出false,因为比较的是它们的地址,每个新创建对象的引用地址都不同。 var box3=null==undefined; document.write(box3+"<br/>");//输出true,因为均为空数值 var box4='2'===2; document.write(box4+"<br/>");//输出false,两个操作数的数据类型不相等 var box5=null===undefined; document.write(box5);//输出false,两个操作数的数据类型不相等
四逻辑运算符
JavaScript语言中的逻辑运算符通常作用于布尔值的操作,一般和关系运算符配合使用,有三个逻辑运算符:&&(逻辑与),||(逻辑或)和!(逻辑非)。
(1)&&表示两边都必须是true,才返回true。
如果两边的操作数有一个操作数不是布尔值的情况下,与运算就不一定返回布尔值,此时遵循下面的规则:
1第一个操作数是对象,则返回第二个操作数
2第二操作数是对象,则第一个操作数返回true,才返回第二个操作数,否则返回false
3一个操作数是null,则返回null
4一个操作数是undefined,则返回undefined
5如果一个运算数是对象,另一个是 Boolean 值,返回该对象
逻辑与运算符属于短路操作,如果有第一个操作数返回的是false,第二个不管是true还是false都返回false。
var box1={}&&(5>4); document.write(box1+"<br/>");//输出true var box2=(5>4)&&{}; document.write(box2+"<br/>");//输出[object Object] var box3=(3>4)&&{}; document.write(box3);//输出false
(2)||表示两边有一个是true,就返回true。
如果两边的操作数有一个操作数不是布尔值的情况下,与运算就不一定返回布尔值,此时遵循下面的规则:
1第一个操作数是对象,则返回第一个个操作数
2第一个操作数的求值结果为fasle,则返回第二个操作数
3两个操作数都是对象,则返回第一个操作数
4两个操作数都是null,则返回null
5两个操作数都是undefined,则返回undefined
6两个操作数都是NaN,则返回NaN
逻辑或运算符也属于短路操作,如果有第一个操作数返回的是true,第二个不管是true还是false都返回true。
var box1={}||(5>4); document.write(box1+"<br/>");//输出[object Object] var box2=(5>4)||{}; document.write(box2+"<br/>");//输出true var box3=(3>4)||{}; document.write(box3);//输出[object Object]
(3)!逻辑非运算符可以作用与任何值,无论这个值是什么数据类型,这个运算符都会返回一个布尔值,它的流程是:先将这个值转换成布尔值,然后取反,规则如下:
1操作数是一个对象,返回false
2操作数是一个空字符串,返回true
3操作数是一个非空字符串,返回false
4操作数是数值0,返回true
5操作数是任意非0数值,返回false
6操作数是null,返回true
7操作数是NaN,返回true
8操作数是undefined,返回true
var box=!{}; document.write(box);//输出false
五、位运算符
JavaScript语言中包括了七种位运算符:~(位非),&(位与),|(位或),^(位异或),<<(左移),>>(有符右移号),>>>(无符号右移)
(1)位非(~)运算把运算数转换成32位数字,然后把二进制数转换成它的二进制反码,最后把二进制数转换成浮点数。实质上是对数字求负,然后减去1即为所得值。
var box=~25; document.write(box);//输出-26
(2)位与(&)运算直接对数字的二进制形式进行运算,然后对上下同一位置的两个数位进行与运算,只有两个数位都为1时才得出1,其余的均为0.
var box=25&3; document.write(box);//输出1
(3)位或(|)运算也是直接对数字的二进制形式进行计算,然后对上下同一位置的两个数位进行或运算,只右两个数位都为0时才得出0,其余的均为1.
var box=25|3; document.write(box);//输出27
(4)位异或(^)也是直接对二进制形式进行运算。当只有一个数位存放的是1时,它才返回1。其余的返回0。也就是两个数位相同时返回0,不同时返回1.
var box=25^3; document.write(box);//输出26
(5)左移运算也是对二进制数进行操作,相等于第一个操作数乘以(2的左移位数次幂)的积。
var box=25<<3; document.write(box);//25左移3位相当于25乘以(2的3次幂),因此输出200
(6)有符号右移运算也是对二进制数进行操作,相等于第一个操作数除以(2的右移位数次幂)的商。
var box=24>>2; document.write(box);//输出6
(7)无符号右移运算也是对二进制数进行操作,对于正数,与有符号右移是相同的结果,但是对于负数,就会所不同。
六、赋值运算符
赋值运算符包括:=(),+=(),-=(),*=(),/=(),%=(),<<=(),>>=(),>>>=()。
var box=100; box+=100;//相当于box=box+100 document.write("box="+box);//输出box=200
七、其他运算符
1)、字符串运算符:“+”,它的作用是将两个字符串想加。规则:只要有一个字符串即可。
var box=100+"10"'; document.write("box="+box);//输出100100
2)、逗号运算符,可以在一条语句中执行多个操作
var box=100,age=200,height=300; document.write("box="+box);//输出box=100
3)、三元操作符:
var box=(3>4)?"对":"错"; document.write(box);//输出错
如果想更详细的了解ECMAScript运算符的知识,可以访问JavaScript高级教程中的ECMASscript一元运算符这个系列中有详细的运算符教程。对于JS的运算符来说,我们可以对比着C++,C#和Java来学,这个还是相当的容易的。
以上就是关于JavaScript的表达式与运算符的全部内容,希望对大家的学习有所帮助。