ES6教程

ES6 运算符

ES6 操作符

操作符可以定义为一个符号,告诉系统执行特定操作。在 JavaScript 中,有一组丰富的运算符,通过使用特定的运算符,您可以执行任何特定任务。
在表达式中使用运算符来评估不同的操作数。
表达式是一种返回值的语句。表达式包括:
Operators: 负责对操作数进行一些操作 操作数: 代表数据。
例如: 假设一个表达式像 x*y。在这个表达式中, x 和y 是操作数,星号(*)是乘法运算符。

运算符的类型

JavaScript 中的运算符可分为:
算术运算符 关系运算符 逻辑运算符 赋值运算符 按位运算符 类型运算符 杂项操作员
让我们尝试详细阐述这些运算符。

算术运算符

算术运算符是可用的基本数学运算符JavaScript ES6、这些运算符负责执行 JavaScript 中的所有数学运算,例如加法、减法等。
运营商 函数
+(添加) 返回操作数的总和
-(减法) 返回操作数之间的差值
*(乘法) 它返回操作数值的乘积。
/(部门) 用于除法,返回商。
%(模数) 它还执行除法并返回余数。
++(增量) 将变量的值加一。
-(递减) 将变量的值减一。
例如
在这个例子中,我们使用了上面列出的所有算术运算符:
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

关系运算符

关系运算符用于比较两个值并根据表达式返回真或假。这些运算符有时称为比较运算符。
操作员 功能
>(大于) 如果左操作数大于右操作数则返​​回真,否则返回假。
<(小于) 如果左操作数小于右操作数则返​​回真,否则返回假。
>=(大于或等于) 如果左操作数大于或等于右操作数则返​​回真,否则返回假。
<=(小于或等于) 如果左操作数小于或等于右操作数则返​​回真,否则返回假。
==(平等) 如果两个操作数的值相同则返回真,否则返回假。
!=(不等于) 如果操作数的值不同则返回真,否则返回假。
例如:
在这个例子中,我们使用了上面列出的所有关系运算符
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

逻辑运算符

逻辑运算符通常用于组合两个或多个关系语句。它们还返回布尔值。
运营商 说明
&&(逻辑与) 如果所有与 && 组合的关系语句都为真,则此运算符返回真,否则返回假。
||(逻辑或) 如果至少有一个与 || 组合的关系语句,则此运算符返回 true为真,否则返回假。
!(逻辑非) 它返回语句结果的倒数。
例如:
在这个例子中,我们使用了上面列出的所有逻辑运算符。
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

赋值运算符

赋值运算符用于为变量赋值。赋值运算符左边的操作数是一个变量,赋值运算符右边的操作数是一个值。
右边的值必须是相同的数据类型左侧变量;否则编译器会报错。
运营商 函数
=(简单赋值) 它只是将右操作数的值赋给左操作数
+=(添加和分配) 此运算符将右操作数的值与左操作数的值相加,并将结果赋给左操作数。
-=(减法和赋值) 此运算符从左操作数的值中减去右操作数的值,并将结果赋给左操作数。
*=(乘法和赋值) 此运算符将右操作数的值乘以左操作数的值,并将结果赋给左操作数。
/=(分而治之) 此运算符将右操作数的值除以左操作数的值,并将结果赋给左操作数。
例如:
在这个例子中,我们使用了上面列出的所有逻辑运算符。
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

按位运算符

按位运算符用于对涉及单个位操作的二进制数字或位模式执行按位运算。按位运算符对参数的二进制表示执行操作
通常,按位运算符使用较少,与应用程序和高性能程序相关。
操作员 说明
按位与(&) 它将第一个操作数的每一位与第二个操作数的相应位进行比较。如果两个位都为 1,则结果位将设置为 1,否则将设置为 0。
按位或(|) 它将第一个操作数的每一位与第二个操作数的相应位进行比较。如果两个位都为 0,则结​​果位将设置为 0,否则将设置为 1、
按位异或(^) 它需要两个操作数,并对两个操作数的每一位进行异或。如果两个位都不同,则返回 1,否则返回 0。
按位非(~) 它翻转其操作数的位,即0变为1,1变为0。
左移(<<) 它将左操作数的值向左移动右操作数指定的位数。
符号传播右移(>>) 它将左操作数的值向右移动右操作数指定的位数。这是符号传播,因为我们从左边添加的位取决于数字的符号(0 表示正,1 表示负)。
零填充右移 它接受两个操作数。第一个操作数指定数字,一个nd 第二个运算符确定要移位的位数。每一位都向右移动,溢出的位将被丢弃。因为0位是从左边添加的,这就是为什么它是一个零填充右移。
例如:
在这个例子中,我们使用了上面列出的所有逻辑运算符。
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
注意: 赋值运算符的逻辑也适用于位运算符,所以它们变成了<<=、>>=、&=、|=、^=。

杂项运算符

这些是在不同情况下执行不同操作的运算符。
运营商 说明
+(连接运算符) 它适用于字符串并将第二个字符串附加到第一个。
-(否定运算符) 它改变了值的符号。
?(条件运算符) 用于表示条件表达式。它也被称为三元运算符。
让我们试着详细了解一下杂项操作符:

否定操作符(-)

用来改变符号
例如:
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
Where,
condition: 指的是条件表达式。
value1: 如果条件为真,则返回此值。
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)
您可以在下表中看到typeof 运算符返回的数据类型和值在 JavaScript 中:
类型 typeof 返回的字符串
String "字符串"
Boolean "布尔值"
Number "数字"
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 运算符给它的类型 undefined 的原因。
如果您在引号内使用布尔值,那么它们将被视为字符串,如您所见 >变量"d"在上面的例子中。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4