注意: 如果您是 TypeScript 新手,请先查看我们的 TypeScript 入门 教程。
TypeScript 运算符
在TypeScript中,运算符是一种特殊符号,用于检查、更改或组合操作数(值)。例如:
let result: number = 2 + 3;
console.log(result);
// Output: 5
在这里,我们使用 + 运算符将两个操作数 2 和 3 相加。
运算符类型
TypeScript 运算符根据其功能进行分类。其中一些是:
- 算术运算符
- 赋值运算符
- 比较运算符
- 逻辑运算符
- 三元运算符
- 类型(typeof)运算符
让我们来详细了解一下。
算术运算符
算术运算符用于执行数学运算。例如:
let product: number = 2 * 4;
console.log(product);
// Output: 8
在这里,我们使用 * 算术运算符计算 2 和 4 的乘积。
一些常用的算术运算符包括:
| 运算符 | 名称 | 示例 |
|---|---|---|
+ |
加法 | 3 + 4 (结果为 7) |
- |
减法 | 5 - 2 (结果为 3) |
* |
乘法 | 2 * 3 (结果为 6) |
/ |
除法 | 4 / 2 (结果为 2) |
% |
余数 | 5 % 2 (结果为 1) |
++ |
递增 | let num: number = 5;++num;// num == 6 |
-- |
递减 | let num: number = 5;num--;// num == 4 |
** |
乘方 | 4 ** 2 (结果为 16) |
示例 1:算术运算符
让我们通过一个示例来了解各种算术运算符的用法。
let num: number = 10;
// Division: divides num by 3
let divisionResult: number = num / 3;
console.log(divisionResult); // Output: 3.3333333333333335
// Remainder: the remainder when num is divided by 3
let remainderResult: number = num % 3;
console.log(remainderResult); // Output: 1
// Increment: increases num by 1
num++;
console.log(num); // Output: 11
// Decrement: decreases num by 1
num--;
console.log(num); // Output: 10
// Exponentiation: raises num to the power of 2
let exponentiationResult: number = num ** 2;
console.log(exponentiationResult); // Output: 100
赋值运算符
赋值运算符用于给变量赋值。例如:
let num: number = 7;
console.log(num);
// Output: 7
在这里,我们使用 = 运算符将值 7 赋给变量 num。
TypeScript 中一些常用的赋值运算符包括:
| 运算符 | 名称 | 示例 |
|---|---|---|
= |
赋值运算符 | let a: number = 7;( a 的值为 7) |
+= |
加法赋值 | a += 5;(相当于 a = a + 5;) |
-= |
减法赋值 | a -= 2;(相当于 a = a - 2;) |
*= |
乘法赋值 | a *= 3;(相当于 a = a * 3;) |
/= |
除法赋值 | a /= 2; (相当于 a = a / 2;) |
%= |
余数赋值 | a %= 4;(相当于 a = a % 4;) |
**= |
乘方赋值 | a **= 2;(相当于 a = a ** 2;) |
示例 2:赋值运算符
让我们通过一个示例来了解各种赋值运算符的用法。
let num: number = 7;
// Using Addition Assignment
num += 5;
console.log("After += 5:", num);
// Using Subtraction Assignment
num -= 2;
console.log("After -= 2:", num);
// Using Multiplication Assignment
num *= 3;
console.log("After *= 3:", num);
// Using Division Assignment
num /= 2;
console.log("After /= 2:", num);
// Using Remainder Assignment
num %= 4;
console.log("After %= 4:", num);
// Using Exponentiation Assignment
num **= 2;
console.log("After **= 2:", num);
输出
After += 5: 12 After -= 2: 10 After *= 3: 30 After /= 2: 15 After %= 4: 3 After **= 2: 9
比较运算符
比较运算符用于比较两个值,并根据比较结果返回一个布尔值(true 或 false)。例如:
let num1: number = 5;
let num2: number = 3;
// Check if num1 is greater than num2
let isGreater: boolean = num1 > num2;
console.log(isGreater);
// Output: true
在这里,我们使用大于(>)比较运算符来比较 num1 和 num2。由于 5 大于 3,表达式 num1 > num2 的计算结果为 true。
一些常用的比较运算符包括:
| 运算符 | 名称 | 示例 |
|---|---|---|
== |
等于 | 3 == 5 (输出:false) |
!= |
不等于 | 3 != 4 (输出:true) |
> |
大于 | 4 > 4 (输出:false) |
< |
小于 | 3 < 3 (输出:false) |
>= |
大于或等于 | 4 >= 4 (输出:true) |
<= |
小于或等于 | 3 <= 3 (输出:true) |
=== |
严格等于 | 3 === "3" (输出:false) |
!== |
严格不等于 | 3 !== "3" (输出:true) |
示例 3:比较运算符
let a: number = 5;
let b: number = 10;
// Equal to
console.log(a == 5); // true
console.log(a == b); // false
// Strictly equal to
console.log(a === 5); // true
// Not equal to
console.log(a != b); // true
// Strictly not equal to
console.log(a !== 10); // true
// Greater than
console.log(b > a); // true
// Less than
console.log(b < a); // false
// Greater than or equal to
console.log(a >= 5); // true
// Less than or equal to
console.log(b <= 10); // true
逻辑运算符
这些运算符通常用于布尔值之间的逻辑运算。逻辑运算符对于基于多个条件做出决策至关重要。例如:
let isLoggedIn: boolean = true;
let hasAdminRights: boolean = false;
// Check if the user is logged in and has admin rights
let canAccessDashboard: boolean = isLoggedIn && hasAdminRights;
console.log(canAccessDashboard);
// Output: false
在这里,我们使用逻辑 AND 运算符(&&)来确定用户是否可以访问管理员仪表板。
由于 isLoggedIn 为 true 而 hasAdminRights 为 false,因此组合条件被计算为 false。
常用的逻辑运算符包括:
| 运算符 | 语法 | 描述 |
|---|---|---|
&& (逻辑 AND) |
expression1 && expression2 |
仅当 expression1 和 expression2 都为 true 时为 true |
|| (逻辑 OR) |
expression1 || expression2 |
当 expression1 或 expression2 中至少有一个为 true 时为 true |
! (逻辑 NOT) |
!expression |
如果 expression 为 true 则为 false,反之亦然 |
示例 4:逻辑运算符
让我们通过一个示例来了解各种逻辑运算符的用法。
// Logical AND example
let hasPassword = true;
let hasUsername = true;
let canLogin = hasPassword && hasUsername;
console.log("Can login:", canLogin);
// Logical OR example
let isWeekday = false;
let isHoliday = true;
let canRelax = isWeekday || isHoliday;
console.log("Can relax:", canRelax);
// Logical NOT example
let isBusy = false;
let isFree = !isBusy;
console.log("Is free:", isFree);
这里,
| 表达式 | 结果 | 原因 |
|---|---|---|
hasPassword && hasUsername |
true |
hasPassword 和 hasUsername 均为 true。 |
isWeekday || isHoliday |
true |
isWeekday 为 false,isHoliday 为 true。 |
!isBusy |
true |
isBusy 为 false,**NOT** 运算符将 false 反转为 true。 |
三元运算符
TypeScript 中的三元运算符,也称为条件运算符,由三部分组成:
- 一个条件。
- 如果条件为
true,则执行的表达式。 - 如果条件为
false,则执行的表达式。
三元运算符的语法是:
condition ? expression1 : expression2
让我们看一个三元运算符的简单示例。
let age: number = 18;
let canVote: string = age >= 18 ? "Yes, you can vote." : "No, you cannot vote.";
console.log(canVote);
输出
Yes, you can vote.
这里,
- 条件:
age >= 18检查age是否为 18 或更大。 - 表达式1: 如果条件为
true,则返回"Yes, you can vote."。 - 表达式2: 如果条件为
false,则返回"No, you cannot vote."。
注意: 三元运算符是 if...else 语句的简写形式。如需了解更多信息,请访问 TypeScript 三元运算符。
typeof 运算符
typeof 运算符用于确定变量或表达式的类型。例如:
let greetings = "Hello, TypeScript!";
let typeOfgreetings = typeof greetings; // Returns 'string'
console.log(typeOfgreetings);
输出
string
在这里,typeof greetings 返回 "string",因为 greetings 初始化为字符串数据。
如需了解有关 typeof 的更多信息,请访问 TypeScript typeof。