📜  ES6运算符

📅  最后修改于: 2021-01-01 03:50:52             🧑  作者: Mango

ES6运算符

可以将运算符定义为告诉系统实施特定操作的符号。在JavaScript中,有很多运算符,通过使用特定的运算符,您可以执行任何特定的任务。

运算符在表达式中用于评估不同的操作数。

表达式是一种返回值的语句。表达式包括:

  • 运算符:它负责对操作数执行一些运算
  • 操作数:它代表数据。

例如:假设像x * y这样的表达式在此表达式中, xy操作数,星号(*)符号是乘法运算符。

运营商类型

JavaScript中的运算符可分为:

  • 算术运算符
  • 关系运算符
  • 逻辑运算符
  • 赋值运算符
  • 按位运算符
  • 类型运算符
  • 杂项运算符

让我们尝试详细说明这些运算符。

算术运算符

算术运算运算符是JavaScript ES6中可用的基本数学运算符。这些运算符负责执行JavaScript中的所有数学运算,例如加法,减法等。

Operators Functions
+ (Addition) It returns the sum of the value of operands
– (Subtraction) It returns the difference between the value of operands
* (Multiplication) It returns the product of operands values.
/ (Division) It is used to perform division, and it returns quotient.
% (Modulus) It also performs division and returns the remainder.
++ (Increment) It increments the value of a variable by one.
– (Decrement) It decrements the value of a variable by one.

例如

在此示例中,我们使用上面列出的所有算术运算运算符:

var x = 30; 
var y = 20 ;

console.log("Addition: " + (x + y) );
console.log("Subtraction: " + (x - y) );
console.log("Multiplication: " + (x * y) );
console.log("The Division will give you the quotient: " + (x / y) );
console.log("Modulus will give you the Remainder: " + (x % y) );
// pre-increment 
console.log("Value of x after pre-increment: "  + (++x) );
// post-increment 
console.log("Value of x after post-increment: " + (x++) );
// pre-decrement 
console.log("Value of y after pre-decrement: "  + (--y) );
// post-decrement 
console.log("Value of y after post-decrement: " + (y--) );

输出:

在终端中执行上述代码时,将获得以下输出:

Addition : 50
Subtraction: 10
Multiplication: 600
The division will give you the quotient: 1.5
Modulus will give you the Remainder: 10
Value of x after pre-increment: 31
Value of x after post-increment: 31
Value of y after pre-decrement: 19
Value of y after post-decrement: 19

关系运算符

关系运算符用于比较两个值,并根据表达式返回true或false。这些运算符有时称为运算符。

Operator Function
> (Greater than) It returns true if the left operand is greater than right operand else it returns false.
< (Less than) It returns true if the left operand is smaller than right operand else it returns false.
>= (Greater than or equal to) It returns true if the left operand is greater than or equal to right operand else it returns false.
<= (Less than or equal to) It returns true if the left operand is smaller than or equal to right operand else it returns false.
== (Equality) It returns true if the value of both operands is the same else it returns false.
!= (Not Equal to) It returns true if the value of operands is not the same else it returns false.

例如:

在此示例中,我们使用上面列出的所有关系运算符

var x = 20; 
var y = 15; 

console.log("Value of x: " + x); 
console.log("Value of y: " + y); 
var result = x > y; 
console.log("x is greater than y: " + result); 
result = x < y; 
console.log("x is smaller than y: " + result); 
result = x >= y; 
console.log("x is greater than or equal to  y: " + result); 
result = x <= y; 
console.log("x is smaller than or equal to y: " + result); 
result = x == y; 
console.log("x is equal to y: " + result); 
result = x != y; 
console.log("x not equal to  y: " + result);

输出:

在终端中执行此代码时,将获得以下输出:

Value of x: 20
Value of y: 15
x is greater than y: true
x is smaller than y: false
x is greater than or equal to  y: true
x is smaller than or equal to y: false
x is equal to y: false
x not equal to  y: true

逻辑运算符

逻辑运算符通常用于组合两个或多个关系语句。它们还返回布尔值。

Operators Description
&& (Logical AND) This operator returns true if all relational statements that are combined with && are true, else it returns false.
|| (Logical OR) This operator returns true if at least one of the relational statements that are combined with || are true, else it returns false.
! (Logical NOT) It returns the inverse of the statement’s result.

例如:

在此示例中,我们使用上面列出的所有逻辑运算符。

var x = 30; 
var y = 80; 

console.log("Value of x = " + x );
console.log("Value of y = " + y );

var result = ((x < 40) && (y <= 90)); 
console.log("(x < 40) && (y <= 90): ", result); 

var result = ((x == 50) || (y > 80)); 
console.log("(x == 50) || (y > 80): ", result); 

var result = !((x > 20) && (y >= 80)); 
console.log("!((x > 20) && (y >= 80)): ", result);

输出:

Value of x = 30
Value of y = 80
(x < 40) && (y <= 90):  true
(x == 50) || (y > 80):  false
!((x > 20) && (y >= 80)):  false

赋值运算符

赋值运算符用于为变量赋值。赋值运算符左侧的操作数是一个变量,而赋值运算符右侧的操作数是一个值。

右侧的值必须与左侧变量的数据类型相同;否则,编译器将引发错误。

Operators Functions
= (Simple Assignment) It simply assigns the value of the right operand to the left operand
+= (Add and Assignment) This operator adds the value of the right operand to the value of the left operand and assigns the result to the left operand.
-= (Subtract and Assignment) This operator subtracts the value of the right operand from the value of the left operand and assigns the result to the left operand.
*= (Multiply and Assignment) This operator multiplies the value of the right operand to the value of the left operand and assigns the result to the left operand.
/= (Divide and Assignment) This operator divides the value of the right operand to the value of the left operand and assigns the result to the left operand.

例如:

在此示例中,我们使用上面列出的所有逻辑运算符。

var x = 20; 
var y = 40; 

x = y;
console.log("After assignment the value of x is:  " + x); 

x += y; 
console.log("x+=y: " + x); 

x -= y; 
console.log("x-=y: " + x); 

x *= y; 
console.log("x*=y: " + x); 

x /= y; 
console.log("x/=y: " + x); 

x %= y; 
console.log("x%=y: " + x);

输出:

After assignment the value of x is:  40 
x+=y: 80
x-=y: 40
x*=y: 1600
x/=y: 40
x%=y: 0

按位运算符

按位运算符用于对涉及单个位操作的二进制数字或位模式执行按位运算。按位运算符对参数的二进制表示形式执行操作

通常,按位运算运算符较少使用,并且与应用程序和超性能程序相关。

Operator Description
Bitwise AND (&) It compares every bit of the first operand to the corresponding bit of the second operand. If both of the bits are 1, then the result bit will set to 1, else it will set to 0.
Bitwise OR (|) It compares every bit of the first operand to the corresponding bit of the second operand. If both of the bits are 0, then the result bit will set to 0, else it will set to 1.
Bitwise XOR (^) It takes two operands and does XOR on each bit of both operands. It returns 1 if both of the two bits are different and returns 0 in any other case.
Bitwise NOT (~) It flips the bits of its operand, i.e., 0 becomes 1 and 1 becomes 0.
Left shift (<<) It shifts the value of the left operand to the left by the number of bits specified by the right operand.
Sign-propagating Right shift (>>) It shifts the value of the left operand to the right by the number of bits specified by the right operand.
This is sign-propagating because the bits that we are adding from the left depends upon the sign of the number (0 represents positive, and 1 represents negative).
Zero-fill right shift It accepts two operands. The first operand specifies the number, and the second operator determines the number of bits to shift. Every bit gets shifted towards the right, and the overflowing bits will be discarded.
Because the 0-bit is added from the left, that’s why it is a zero-fill right shift.

例如:

在此示例中,我们使用上面列出的所有逻辑运算符。

var x = 70;    /* 70 = 0100 0110 */
var y = 80;    /* 80 = 0101 0000 */
var res = 0;

console.log("Value of 70 in binary 0100 0110" );
console.log("Value of 80 in binary 0101 0000" );


res = x & y;       /* 64 = 0100 0000 */
console.log("Value of x & y = %d\n", res );

res = x | y;       /* 86 = 0101 0110 */
console.log("Value of x | y = %d\n", res );

res = x ^ y;       /* 22 = 0001 0110 */
console.log("Value of x ^ y = %d\n", res );

res = ~x;          /*-71 = -0100 0111 */
console.log("Value of ~ x = %d\n", res );

res = x << 2;     /* 280 = 1000 11000 */
console.log("Value of x << 2 = %d\n", res );
res = x >> 2;     /* 17 = 0001 0001 */
console.log("Value of x >> 2 = %d\n", res );

输出:

Value of 70 in binary 0100 0110
Value of 80 in binary 0101 0000
Value of x & y = 64

Value of x | y = 86

Value of x ^ y = 22

Value of ~ x = -71

Value of x << 2 = 280

Value of x >> 2 = 17

注意:赋值运算符的逻辑也适用于按位运算符,因此它们变为<< =,>> =,&=,| =,^ =。

杂项运算符

这些是在不同情况下执行不同操作的运算符。

Operators Description
+ (Concatenation Operator) It applies to strings and appends the second string to first.
– (Negation Operator) It changes the sign of the value.
? (Conditional Operator) It is used for representing the conditional expression. It is also called a ternary operator.

让我们尝试详细了解其他运算符:

求反运算符(-)

用于更改值的符号。

例如:

var num1 = 80;
var num2 = -num1;
console.log("Value of num1 = " +num1); // It will give 80
console.log("Value of num2 = " +num2); // It will give -80

输出:

Value of num1 = 80
Value of num2 = -80

串联运算符(+)

它适用于字符串,并将第二个字符串附加到第一个字符串。您可以通过以下示例了解它:

例:

var str1 = 'Hello' + 'World';
var str2 = 'Welcome ' + 'Back';
console.log(str1);
console.log(str2);

输出:

HelloWorld
Welcome Back

串联运算符不会在字符串之间添加空格。它在单个语句中连接多个字符串。如果要显示字符串之间的间隔,则必须手动定义它。在上面的示例中,字符串“ HelloWorld”不包含任何空格,但是第二个字符串“ Welcome Back”具有空格,因为我们已手动添加了它。

条件运算符(?)

该运算符表示条件表达式。也称为“三元运算符”。

句法:

variablename = (condition) ? value1 : value2

哪里,

condition:它是指条件表达式。

value1:如果条件为true,则将返回此值。

value2:如果条件为假,则将返回此值。

例:

var num1 = 30;
var num2 = 20;
var res = num1 > num2 ? "Yes 30 is greater than 20" : "No It's not";
console.log(res);

输出:

Yes 30 is greater than 20

类型运算符

它是一元运算运算符,返回操作数的数据类型。

句法:

typeof(operand)

您可以在下表中看到由运算符返回的数据类型和值:

Type String Returned by typeof
String “string”
Boolean “boolean”
Number “number”
Object “object”

例:

var a = 20;
var b = true;
var c = 'Hello';
var d = 'true';
var e;
console.log("Variable a is " +typeof(a));
console.log("Variable b is " +typeof(b));
console.log("Variable c is a " +typeof(c));
console.log("Variable d is a " +typeof(d));
console.log("Variable e is " +typeof(e));

输出:

Variable a is number
Variable b is boolean
Variable c is a string
Variable d is a string
Variable e is undefined

在上面的示例中,变量e未定义(或未初始化);这就是为什么typeof运算符赋予其类型未定义的原因。

如果您在引号内使用布尔值,则它们将被视为字符串,因为您可以在上面的示例中看到变量“ d”