欢迎来到学习 JavaScript 的第三天!今天,我们将探索运算符和表达式——在程序中执行计算、制定决策和编写有意义的逻辑的基本工具。
运算符是对值或变量执行运算的特殊符号或关键字。这些操作的范围可以从算术计算到逻辑决策。
用于数学运算,如加法、减法、乘法等
Operator | Description | Example | Output |
---|---|---|---|
Addition | 5 3 | 8 | |
- | Subtraction | 10 - 6 | 4 |
* | Multiplication | 4 * 2 | 8 |
/ | Division | 12 / 4 | 3 |
% | Modulus (Remainder) | 10 % 3 | 1 |
** | Exponentiation | 2 ** 3 | 8 |
let num1 = 10; let num2 = 3; console.log(num1 + num2); // 13 console.log(num1 % num2); // 1
Operator | Description | Example | Output |
---|---|---|---|
== | Equal to | 5 == "5" | true |
=== | Strict equal to | 5 === "5" | false |
!= | Not equal to | 5 != "5" | false |
!== | Strict not equal | 5 !== "5" | true |
< | Less than | 5 < 10 | true |
> | Greater than | 10 > 5 | true |
<= | Less than or equal | 5 <= 5 | true |
>= | Greater than or equal | 10 >= 5 | true |
let num1 = 10; let num2 = 3; console.log(num1 + num2); // 13 console.log(num1 % num2); // 1
组合多个条件或反转逻辑。
Operator | Description | Example | Output |
---|---|---|---|
&& | Logical AND | true && false | false |
` | ` | Logical OR | |
! | Logical NOT | !true | false |
let age = 20; console.log(age >= 18); // true console.log(age === "20"); // false
Operator | Description | Example | Output |
---|---|---|---|
= | Assign | x = 10 | 10 |
= | Add and assign | x = 5 | x = x 5 |
-= | Subtract and assign | x -= 3 | x = x - 3 |
*= | Multiply and assign | x *= 2 | x = x * 2 |
/= | Divide and assign | x /= 2 | x = x / 2 |
%= | Modulus and assign | x %= 3 | x = x % 3 |
let isAdult = true; let hasID = false; console.log(isAdult && hasID); // false console.log(isAdult || hasID); // true
Operator | Description | Precedence |
---|---|---|
** | Exponentiation | 1 |
*, /, % | Multiplication, Division, Modulus | 2 |
, - | Addition, Subtraction | 3 |
<, >, ==, != | Comparison | 4 |
&& | Logical AND | 5 |
` | ` |
let num1 = 10; let num2 = 3; console.log(num1 + num2); // 13 console.log(num1 % num2); // 1
使用括号 () 控制优先级。
let age = 20; console.log(age >= 18); // true console.log(age === "20"); // false
今天,我们介绍了:
在第 4 天,我们将学习 JavaScript 中的控制流,重点关注条件语句和循环。敬请期待 2024 年 12 月 11 日!
以上是JavaScript 中的运算符和表达式的详细内容。更多信息请关注PHP中文网其他相关文章!