📅  最后修改于: 2021-01-11 12:23:02             🧑  作者: Mango
运算符是对值或数据进行运算的符号。它表示处理数据的特定操作。运算符操作的数据称为操作数。它可以与一个或多个值一起使用以产生单个值。 TypeScript程序可使用所有标准JavaScript运算符。
10 + 10 = 20;
在上面的示例中,值“ 10”和“ 20”被称为操作数,而值“ +”和“ =”被称为运算符。
在TypeScript中,可以将运算符分为以下几种方式。
算术运算运算符将数字值作为其操作数,执行一个操作,然后返回一个数字值。最常见的算术运算运算符是加法(+),减法(-),乘法(*)和除法(/)。
Operator | Operator_Name | Description | Example |
---|---|---|---|
+ | Addition | It returns an addition of the values. |
let a = 20; let b = 30; let c = a + b; console.log( c ); // Output 30 |
– | Subtraction | It returns the difference of the values. |
let a = 30; let b = 20; let c = a - b; console.log( c ); // Output 10 |
* | Multiplication | It returns the product of the values. |
let a = 30; let b = 20; let c = a * b; console.log( c ); // Output 600 |
/ | Division | It performs the division operation, and returns the quotient. |
let a = 100; let b = 20; let c = a / b; console.log( c ); // Output 5 |
% | Modulus | It performs the division operation and returns the remainder. |
let a = 95; let b = 20; let c = a % b; console.log( c ); // Output 15 |
++ | Increment | It is used to increments the value of the variable by one. |
let a = 55; a++; console.log( a ); // Output 56 |
— | Decrement | It is used to decrements the value of the variable by one. |
let a = 55; a--; console.log( a ); // Output 54 |
运算符用于将两个操作数进行比较。这些运算符返回布尔值true或false。最重要的比较运算符如下。
Operator | Operator_Name | Description | Example |
---|---|---|---|
== | Is equal to | It checks whether the values of the two operands are equal or not. |
let a = 10; let b = 20; console.log(a==b); //false console.log(a==10); //true console.log(10=='10'); //true |
=== | Identical(equal and of the same type) | It checks whether the type and values of the two operands are equal or not. |
let a = 10; let b = 20; console.log(a===b); //false console.log(a===10); //true console.log(10==='10'); //false |
!= | Not equal to | It checks whether the values of the two operands are equal or not. |
let a = 10; let b = 20; console.log(a!=b); //true console.log(a!=10); //false console.log(10!='10'); //false |
!== | Not identical | It checks whether the type and values of the two operands are equal or not. |
let a = 10; let b = 20; console.log(a!==b); //true console.log(a!==10); /false console.log(10!=='10'); //true |
> | Greater than | It checks whether the value of the left operands is greater than the value of the right operand or not. |
let a = 30; let b = 20; console.log(a>b); //true console.log(a>30); //false console.log(20> 20'); //false |
>= | Greater than or equal to | It checks whether the value of the left operands is greater than or equal to the value of the right operand or not. |
let a = 20; let b = 20; console.log(a>=b); //true console.log(a>=30); //false console.log(20>='20'); //true |
< | Less than | It checks whether the value of the left operands is less than the value of the right operand or not. |
let a = 10; let b = 20; console.log(a |
<= | Less than or equal to | It checks whether the value of the left operands is less than or equal to the value of the right operand or not. |
let a = 10; let b = 20; console.log(a<=b); //true console.log(a<=10); //true console.log(10<='10'); //true |
逻辑运算符用于将两个或多个条件组合为单个表达式,并返回布尔结果true或false。逻辑运算符如下。
Operator | Operator_Name | Description | Example |
---|---|---|---|
&& | Logical AND | It returns true if both the operands(expression) are true, otherwise returns false. |
let a = false; let b = true; console.log(a&&b); /false console.log(b&&true); //true console.log(b&&10); //10 which is also 'true' console.log(a&&'10'); //false |
|| | Logical OR | It returns true if any of the operands(expression) are true, otherwise returns false. |
let a = false; let b = true; console.log(a||b); //true console.log(b||true); //true console.log(b||10); //true console.log(a||'10'); //'10' which is also 'true' |
! | Logical NOT | It returns the inverse result of an operand(expression). |
let a = 20; let b = 30; console.log(!true); //false console.log(!false); //true console.log(!a); //false console.log(!b); /false console.log(!null); //true |
按位运算运算符对操作数执行按位运算。按位运算运算符如下。
Operator | Operator_Name | Description | Example |
---|---|---|---|
& | Bitwise AND | It returns the result of a Boolean AND operation on each bit of its integer arguments. |
let a = 2; let b = 3; let c = a & b; console.log(c); // |
| | Bitwise OR | It returns the result of a Boolean OR operation on each bit of its integer arguments. |
let a = 2; let b = 3; let c = a | b; console.log(c); // |
^ | Bitwise XOR | It returns the result of a Boolean Exclusive OR operation on each bit of its integer arguments. |
let a = 2; let b = 3; let c = a ^ b; console.log(c); // Output 1 |
~ | Bitwise NOT | It inverts each bit in the operands. |
let a = 2; let c = ~ a; console.log(c); // Output -3 |
>> | Bitwise Right Shift | The left operand’s value is moved to the right by the number of bits specified in the right operand. |
let a = 2; let b = 3; let c = a >> b; console.log(c); // Output 0 |
<< | Bitwise Left Shift | The left operand’s value is moved to the left by the number of bits specified in the right operand. New bits are filled with zeroes on the right side. |
let a = 2; let b = 3; let c = a << b; console.log(c); // Output 16 |
>>> | Bitwise Right Shift with Zero | The left operand’s value is moved to the right by the number of bits specified in the right operand and zeroes are added on the left side. |
let a = 3; let b = 4; let c = a >>> b; console.log(c); // Output 0 |
赋值运算符用于为变量赋值。赋值运算符的左侧称为变量,而赋值运算符的右侧称为值。变量和值的数据类型必须相同,否则编译器将引发错误。赋值运算符如下。
Operator | Operator_Name | Description | Example |
---|---|---|---|
= | Assign | It assigns values from right side to left side operand. |
let a = 10; let b = 5; console.log("a=b:" +a); // Output 10 |
+= | Add and assign | It adds the left operand with the right operand and assigns the result to the left side operand. |
let a = 10; let b = 5; let c = a += b; console.log(c); // Output 15 |
-= | Subtract and assign | It subtracts the right operand from the left operand and assigns the result to the left side operand. |
let a = 10; let b = 5; let c = a -= b; console.log(c); // Output 5 |
*= | Multiply and assign | It multiplies the left operand with the right operand and assigns the result to the left side operand. |
let a = 10; let b = 5; let c = a *= b; console.log(c); // Output 50 |
/= | Divide and assign | It divides the left operand with the right operand and assigns the result to the left side operand. |
let a = 10; let b = 5; let c = a /= b; console.log(c); // Output 2 |
%= | Modulus and assign | It divides the left operand with the right operand and assigns the result to the left side operand. |
let a = 16; let b = 5; let c = a %= b; console.log(c); // Output 1 |
条件运算符采用三个操作数,并根据条件返回布尔值,无论该条件是true还是false。它的工作类似于if-else语句。条件运算符具有从右到左的关联性。下面给出了条件运算符的语法。
expression ? expression-1 : expression-2;
let num = 16;
let result = (num > 0) ? "True":"False"
console.log(result);
输出:
True
串联(+)运算符符是用于附加两个字符串的运算符。在串联操作中,我们不能在字符串之间添加空格。我们可以在一个语句中连接多个字符串。以下示例帮助我们理解TypeScript中的串联运算符。
let message = "Welcome to " + "JavaTpoint";
console.log("Result of String Operator: " +message);
输出:
Result of String Operator: Welcome to JavaTpoint
有可用的运算符集合,可以在使用TypeScript中的对象时为您提供帮助。类型运算符的示例包括typeof,instanceof,in和delete等运算符。这些运算符的详细说明如下。
Operator_Name | Description | Example |
---|---|---|
in | It is used to check for the existence of a property on an object. |
let Bike = {make: 'Honda', model: 'CLIQ', year: 2018}; console.log('make' in Bike); // Output: true |
delete | It is used to delete the properties from the objects. |
let Bike = { Company1: 'Honda', Company2: 'Hero', Company3: 'Royal Enfield' }; delete Bike.Company1; console.log(Bike); // Output: { Company2: 'Hero', Company3: 'Royal Enfield' } |
typeof | It returns the data type of the operand. |
let message = "Welcome to " + "JavaTpoint"; console.log(typeof message); // Output: String |
instanceof | It is used to check if the object is of a specified type or not. |
let arr = [1, 2, 3]; console.log( arr instanceof Array ); // true console.log( arr instanceof String ); // false |