首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Empty

空语句用来表明没有语句,尽管 JavaScript 语法希望有语句。

语法

代码语言:javascript
复制
;

描述

空语句是一个分号(;),表示不会执行任何语句,即使 JavaScript 语法需要一个语句。相反,当你需要多行语句,但 JavaScript 只允许一个时,可以使用语句块;语句块可以将多条语句合并为一个。

示例

空语句有时与循环语句一起使用。以下示例使用空循环体:

代码语言:javascript
复制
var arr = [1, 2, 3];

// Assign all array values to 0
for (i = 0; i < arr.length; arr[i++] = 0) /* empty statement */ ;

console.log(arr)
// [0, 0, 0]

提示:在使用空语句的情况下专门写上注释是个不错的主意,因为不是很容易区分空语句和普通的分号。下面的示例可能不是故意加上分号的:

代码语言:javascript
复制
if (condition);       // Caution, this "if" does nothing!
   killTheUniverse()  // So this always gets executed!!!

另一个例子:if...else?语句不带花括号({})。如果threetrue, 不会发生任何事,four不会执行,同时else从句中的launchRocket()函数也不会执行。

代码语言:javascript
复制
if (one)
  doOne();
else if (two)
  doTwo();
else if (three)
  ; // nothing here
else if (four)
  doFour();
else
  launchRocket();

规范

Specification

Status

Comment

ECMAScript Latest Draft (ECMA-262)The definition of 'Empty statement' in that specification.

Draft

?

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'Empty statement' in that specification.

Standard

?

ECMAScript 5.1 (ECMA-262)The definition of 'Empty statement' in that specification.

Standard

?

ECMAScript 3rd Edition (ECMA-262)The definition of 'Empty statement' in that specification.

Standard

?

ECMAScript 1st Edition (ECMA-262)The definition of 'Empty statement' in that specification.

Standard

Initial definition.

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

Feature

Android

Chrome for Android

Edge

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

(Yes)

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com