📅  最后修改于: 2020-10-25 10:29:27             🧑  作者: Mango
表达式是一种特殊的声明,可以求值。每个表达式都由-
操作数-表示数据。
运算符-定义如何处理操作数以产生值。
考虑下面的表达式2 +3。在表达式中,2和3是操作数,符号+(加号)是运算符。 JavaScript支持以下类型的运算符-
假设变量a和b中的值分别为10和5。
Operator | Function | Example |
---|---|---|
+ | Addition
Returns the sum of the operands. |
a + b is 15 |
– | Subtraction
Returns the difference of the values. |
a-b is 5 |
* | Multiplication
Returns the product of the values. |
a*b is 50 |
/ | Division
Performs a division operation and returns the quotient. |
a/b is 2 |
% | Modulus
Performs a division and returns the remainder. |
a%b is 0 |
++ | Increment
Increments the value of the variable by one. |
a++ is 11 |
— | Decrement
Decrements the value of the variable by one. |
a– is 9 |
关系运算符测试或定义两个实体之间的关系类型。关系运算符返回布尔值,即true / false。
假设A的值为10,B的值为20。
Operators | Description | Example |
---|---|---|
> | Greater than | (A > B) is False |
< | Lesser than | (A < B) is True |
>= | Greater than or equal to | (A >= B) is False |
<= | Lesser than or equal to | (A <= B) is True |
== | Equality | (A == B) is False |
!= | Not Equal | (A!= B) is True |
逻辑运算符用于组合两个或多个条件。逻辑运算符也返回布尔值。假设变量A的值为10,变量B的值为20。
显示示例。
Operators | Description | Example |
---|---|---|
&& |
And
The operator returns true only if all the expressions specified return true. |
(A > 10 && B > 10) is False |
|| |
Or
The operator returns true if at least one of the expressions specified return true. |
(A > 10 || B > 10) is True |
! |
Not
The operator returns the inverse of the expression’s result. For E.g.: !(7>5) returns false. |
!(A > 10) is True |
JavaScript支持以下按位运算运算符。下表总结了JavaScript的按位运算运算符。
显示示例。
Operators | Usage | Description |
---|---|---|
Bitwise AND | a & b | Returns a one in each bit position for which the corresponding bits of both operands are ones |
Bitwise OR | a | b | Returns a one in each bit position for which the corresponding bits of either or both operands are ones |
Bitwise XOR | a^b | Returns a one in each bit position for which the corresponding bits of either but not both operands are ones |
Bitwise NOT | ~ a | Inverts the bits of its operand |
Left shift | a << b | Shifts a in binary representation b (< 32) bits to the left, shifting in zeroes from the right |
Sign-propagating right shift | a >> b | Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off |
Zero-fill right shift | a >>> b | Shifts a in binary representation b (< 32) bits to the right, discarding bits shifted off, and shifting in zeroes from the left |
下表总结了赋值运算符。
显示示例。
Sr.No | Operator & Description |
---|---|
1 |
= (Simple Assignment) Assigns values from the right side operand to the left side operand. Example − C = A + B will assign the value of A + B into C |
2 |
+= (Add and Assignment) It adds the right operand to the left operand and assigns the result to the left operand. Example − C += A is equivalent to C = C + A |
3 |
-= (Subtract and Assignment) It subtracts the right operand from the left operand and assigns the result to the left operand. Example C -= A is equivalent to C = C – A |
4 |
*= (Multiply and Assignment) It multiplies the right operand with the left operand and assigns the result to the left operand. Example C *= A is equivalent to C = C * A |
5 |
/= (Divide and Assignment) It divides the left operand with the right operand and assigns the result to the left operand. |
注–相同的逻辑适用于按位运算符,因此它们将变为<< =,>> =,>> =,&=,| =和^ =。
以下是一些其他运算符。
更改值的符号。以下程序是相同的示例。
var x = 4
var y = -x;
console.log("value of x: ",x); //outputs 4
console.log("value of y: ",y); //outputs -4
成功执行上述程序后,将显示以下输出。
value of x: 4
value of y: -4
+运算符应用于字符串,会将第二个字符串附加到第一个字符串。以下程序有助于理解此概念。
var msg = "hello"+"world"
console.log(msg)
成功执行上述程序后,将显示以下输出。
helloworld
串联操作不会在字符串之间添加空格。可以在单个语句中连接多个字符串。
该运算符用于表示条件表达式。条件运算符有时也称为三元运算符。以下是语法。
Test ? expr1 : expr2
哪里,
测试-引用条件表达式
expr1-如果条件为真,则返回值
expr2-如果条件为假,则返回值
例
var num = -2
var result = num > 0 ?"positive":"non-positive"
console.log(result)
第2行检查变量num中的值是否大于零。如果num设置为大于零的值,则返回字符串“正”,否则返回“非正”字符串。
成功执行上述程序后,将显示以下输出。
non-positive
它是一元运算运算符。该运算符返回操作数的数据类型。下表列出了JavaScript中typeof运算符返回的数据类型和值。
Type | String Returned by typeof |
---|---|
Number | “number” |
String | “string” |
Boolean | “boolean” |
Object | “object” |
下面的示例代码将数字显示为输出。
var num = 12
console.log(typeof num); //output: number
成功执行上述代码后,将显示以下输出。
number
ES6提供了一个新的运算符,称为散布运算符。扩展运算符由三个点“ …”表示。散布运算符将数组转换为单个数组元素。
以下示例说明了函数中扩展运算符的用法
上面代码的输出如下所示-
sum is : 60
sum is 6
散布运算符可用于将一个数组复制到另一个数组中。它也可以用来连接两个或多个数组。这在下面的示例中显示-
以上代码的输出将如下所示-
[10, 20, 30]
[10, 20, 30, 40, 50, 60]
散布运算符可用于将一个对象复制到另一个对象。它也可以用来连接两个或多个对象。这在下面的示例中显示-
上面代码的输出如下:
{firstName: "Mohtashim", company: "TutorialsPoint"}
{firstName: "Mohtashim", company: "TutorialsPoint", lastName: "Mohammad"}