📜  ES6 |运营商

📅  最后修改于: 2022-05-13 01:56:25.538000             🧑  作者: Mango

ES6 |运营商

表达式是一种特殊类型的求值语句。每个表达式由

  • 操作数:表示数据。
  • 运算符:对操作数执行某些操作。

考虑以下表达式 - 2 / 3 ,在表达式中,2 和 3 是操作数,符号/是运算符。
JavaScript 支持以下类型的运算符:

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

算术运算符:众所周知,这些是 JavaScript ES6 中可用的基本数学运算符。

OperatorFunction
Addition(+)Returns sum of the operands.
Subtraction(-)Returns the difference of the values.
Multiplication(*)Returns the product of the values.
Division(/)Performs division and returns the quotient.
Modulus(%)Performs division and returns the remainder.
Increment(++)Increases the value of the variable by one.
Decrement(- -)Decreases the value of the variable by one.

例子:

javascript


javascript


javascript


javascript


javascript


javascript


javascript


输出:

Sum : 13
Difference : 8
Product : 26.25
Quotient : 4.2
Remainder : 0.5
Value of num1 after pre-increment : 11.5
Value of num1 after post-increment : 11.5
Value of num2 after pre-decrement : 1.5
Value of num2 after post-decrement : 1.5

关系运算符:比较两个值的运算符。关系运算符有时称为运算符。

OperatorFunction
>Returns true if the left operand is greater than right else, false.
<Returns true if the left operand is lesser than right else, false.
>=Returns true if the left operand is greater than or equal to right else, false.
<=Returns true if the left operand is lesser than or equal to right else, false.
==Returns true if both the operands are same else, false./td>
==Returns true if both the operands are the same else, false.
!=Returns true if both the operands are not same else, false.

例子:

javascript


输出:

11>12 : false
11=11 : true
12<=12 : true
11==11 : true
11!=11 : false

逻辑运算符:逻辑运算符用于组合两个或多个关系语句。

OperatorFunction
And(&&)Return true if all the relational statements combined with && are true, else false.
Or(||)Return true if at least one of the relational statements combined with || is true, else false.<
!Returns the inverse of the relational statement’s result.

例子:

javascript

            

输出:

13>12 && 12>11 && 9==9 : true
11>12 && 12>11 && 9==9 : false
11>12 || 12>11 || 9==9 : true
11>12 && (12>11 || 9==9) : false

按位运算符:按位运算运算符是一种运算符,用于对位模式或二进制数字执行按位运算,涉及对各个位的操作。

OperatorFunction
Bitwise AND(&)Compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 0, the corresponding result bit is set to 0, else 1.
Bitwise OR(|)Compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1, the corresponding result bit is set to 1, else 0.
Bitwise XOR(^)Inverts the bits of its corresponding operand
Left shift(<<)Will shift the ‘n’ number of bits to the left side, and n bits with the value 0 will be filled on the right side. Example: x=2, t=4, x<.
Right shift(>>)Will shift the ‘n’ number of bits to the right side, and ‘n’ bits with the value 0 will be filled on the left side. Example: x=2, t=4, x>>t, for easy evaluation it performs t/(2^n)=2/(2^4) .
Zero-fill right shiftShifts a in binary representation b (< 32) bits to the right, discarding bits shifted off, and shifting in zeroes from the left

例子:

javascript


输出:

2&3 : 2
2|3 : 3
2^3 : 1
~4 : -5
2<>3 : 0

赋值运算符:赋值运算符运算符用于为变量、属性、事件或索引器元素分配新值的运算符。

OperatorFunction
Simple Assignment(=)Assigns the value of the right operand to left operand.
Add and Assignment(+=)It adds the right operand to the left operand and assigns the result to the left operand.
Subtract and Assignment(-=)It subtracts the right operand from the left operand and assigns the result to the left operand.
Multiply and Assignment(*=)It multiplies the right operand with the left operand and assigns the result to the left operand.
Divide and Assignment(/=)It divides the left operand with the right operand and assigns the result to the left operand.

例子:

javascript


输出:

a = b : 10
a += b : 20
a -= b : 10
a *= b : 100
a /= b : 10

类型运算符:它是一元运算运算符。该运算符返回操作数的数据类型。
句法:

typeof(operand)

例子:

javascript


输出:

a is number
b is string
c is : boolean
d is : function

杂项运算符:这些运算符符在不同类型的场合使用时会执行不同的运算符。

OperatorFunction
Negation operator (-)Changes the sign of a value.
Concatenation operator (+)+, when applied to strings it appends them.
Conditional Operator (?)It can use as a ternary operator.

例子:

javascript


输出:

a+b : GeeksforGeeks=>A Computer science portal.
2>3 :No, It is not.
d = -d : -9