表达式
一句话解释
表达式 是 任何 片段 的 code 那个 evaluates 到 单个 值, 如 2 + 3 or "Hi, " + name.
用大白话说
表达式是 片段的 code 那个 produces 值. 2 + 3 是表达式 (它 evaluates 到 5). "Hi, " + name 是表达式 (它 evaluates 到字符串). 偶数 bare 变量如 age is an expression — it evaluates to the value the variable holds.
Expressions can be combined and nested. 添加(2, 3) * 4 is an expression made of a function call expression, a literal expression, and a multiplication. Anywhere JavaScript expects a value, you can put an expression: in variable assignments, function arguments, array elements, and more.
The counterpart to an expression is a statement — a complete unit of code that performs an action, like 如果, for, or 返回. Statements do not produce values. Most programs are a mix of statements that contain expressions.
示例
// Each of these is an expression that produces a value:
5;
"hello";
2 + 3;
Math.max(1, 2);
[1, 2, 3].length;
// Expressions can be used anywhere a value is expected:
const total = 10 + 5; // 10 + 5 is an expression
console.log(2 * 3 + 1); // 2 * 3 + 1 is an expression每个 行 above 是 表达式 — 它 evaluates 到 值. 表达式 能 是 nested 内部 other 表达式.
常见混淆
容易混淆的对象: 语句
关键区别: 表达式 evaluates 到 值 (2 + 3 → 5); 语句 执行 操作 and 做 不 produce 值 (如果, for, 返回). 一些 constructs 如
x = 5是 两者都 — 它们 执行 assignment and evaluate 到 assigned 值.
相关术语
相关资源
相关课程
- 变量
表达式 是 assigned 到 变量
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。