表达式
一句话解释
表达式是 任何片段的 code 那个 evaluates 到单个值, 如 2 + 3 or x * 10.
用大白话说
表达式是 combination 的值, 变量, and 运算符那个 computer evaluates 到 produce 单个 result. 2 + 3 是表达式那个 evaluates 到 5. name + "!" is an expression that evaluates to a string.
Expressions can be simple (a single value like 42 or "hello") or complex (nested operations like (+ b) * c / 2). They can appear anywhere a value is expected: in assignments, function arguments, conditions, and return statements.
The key distinction is: an expression PRODUCES a value; a statement DOES something. let x = 5 is a statement (it declares a variable). 5 is an expression (it evaluates to 5). x + 1 is an expression (it evaluates to 6 if x is 5).
示例
let x = 5;
// These are all expressions:
console.log(42); // 42
console.log(x + 3); // 8
console.log(x > 0); // true
console.log("Hi, " + "Ada"); // "Hi, Ada"每个实参到 console.log 是表达式那个 evaluates 到值 之前是 printed.
常见混淆
容易混淆的对象: 语句
关键区别: 表达式 evaluates 到值 (2 + 3 → 5); 语句执行操作 (let x = 5;). 一些 constructs 是两者都: x++ 是两者都表达式 and 语句.
相关术语
相关资源
相关课程
- 变量
表达式 use 变量 and 运算符.
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。