空声明
空语句用于不提供任何语句,尽管 JavaScript 语法期望提供一个语句。
¥An empty statement is used to provide no statement, although the JavaScript syntax would expect one.
Try it
语法
描述
¥Description
空语句是一个分号 (;
),表示不会执行任何语句,即使 JavaScript 语法需要一个语句。
¥The empty statement is a semicolon (;
) indicating that no statement will
be executed, even if JavaScript syntax requires one.
相反的行为,即你想要多个语句,但 JavaScript 只允许单个语句,可以使用 块语句 来实现,它将多个语句组合成一个语句。
¥The opposite behavior, where you want multiple statements, but JavaScript only allows a single one, is possible using a block statement, which combines several statements into a single one.
示例
空循环体
¥Empty loop body
空语句有时与循环语句一起使用。请参阅以下带有空循环体的示例:
¥The empty statement is sometimes used with loop statements. See the following example with an empty loop body:
const arr = [1, 2, 3];
// Assign all array values to 0
for (let i = 0; i < arr.length; arr[i++] = 0) /* empty statement */ ;
console.log(arr);
// [0, 0, 0]
无意使用
¥Unintentional usage
对有意使用空语句进行注释是个好主意,因为它与普通分号的区别并不明显。
¥It is a good idea to comment intentional use of the empty statement, as it is not really obvious to distinguish from a normal semicolon.
在下面的示例中,这种用法可能不是有意的:
¥In the following example, the usage is probably not intentional:
if (condition); // Caution, this "if" does nothing!
killTheUniverse(); // So this always gets executed!!!
规范
Specification |
---|
ECMAScript Language Specification # sec-empty-statement |
浏览器兼容性
BCD tables only load in the browser
也可以看看
¥See also