语句和声明

JavaScript 应用程序是由许多语法正确的语句组成的。单个语句可以跨多行。如果每个语句用分号隔开,那么多个语句可以在一行中出现。本页的内容并不是一个关键字,而是一组关键字。

语句和声明(按类别分类)

若需要按字母顺序排列的列表,请参阅左侧边栏。

控制流程

Block
一个块语句可以用来管理零个或多个语句。该区块是由一对大括号分隔。
break
终止当前的循环,switch 或 label 语句,使程序跳到下一个语句执行。
continue
终止执行当前或标签循环的语句,直接执行下一个迭代循环。
Empty
空语句用来表示没有语句的情况,尽管 JavaScript 语法期望有语句提供。
if...else
如果指定的条件是 true ,则执行相匹配的一个语句,若为 false,则执行另一个语句。
switch
计算表达式,将子句于表达式的值做匹配,执行与该值相关联的语句。
throw
抛出一个用户定义的异常。
try...catch
标记一个语句块,并指定一个应该抛出异常的反馈。(Marks a block of statements to try, and specifies a response, should an exception be thrown.)

声明

var
声明一个变量,可同时将其初始化为一个值。
let
声明一个块级本地变量,可同时将其初始化为一个值。
const
声明一个只读的命名常量。

函数和类

function
声明一个指定参数的函数。
function*
生成器函数使 迭代器更容易使用。
async function
使用指定的参数声明一个异步函数。
return
指定函数的返回值。
class
声明一个类。

迭代器

do...while
创建一个循环来执行语句,直到该语句条件表达式的值为 false。先执行语句,再执行条件表达式,该语句至少会执行一次。
for
创建一个由3个可选的表达式组成的循环,该循环用括号包裹,分号分割,并在循环体中执行语句。
for each...in
通过指定的变量迭代对象所有属性的值。针对每个唯一的属性,会执行指定的语句块。
for...in
无序遍历对象的可枚举属性。语句针对每个唯一的属性。
for...of
遍历可迭代的对象(包括 数组、类数组对象、 迭代器和生成器),对每个不同属性的属性,调用一个自定义的有执行语句的迭代钩子。
for await...of
在异步可迭代对象、类数组对象、 迭代器和生成器上迭代,调用自定义迭代钩子,其中包含要为每个不同属性的值执行的语句。
while
创建一个循环语句,循环会一直持续到该语句条件表达式的值为false。先执行条件表达式,然后执行语句。

其他

debugger
调用可用的调试功能。如果没有调试功能可用,该语句不生效。
export
用来导出函数,以便这些函数能够被导入到外部模块或其他脚本中。
import
用来引入外部的模块或另一个script中导出的函数。
import.meta
向 JavaScript 模块公开上下文特定的元数据的元属性。
label
带标识的语句,与 breakcontinue 语句一起使用。
with
拓展一个语句的作用域。

规范

规范 状态 备注
ECMAScript 1st Edition (ECMA-262)
Statements
Standard Initial definition
ECMAScript 3rd Edition (ECMA-262)
Statements
Standard
ECMAScript 5.1 (ECMA-262)
Statements
Standard
ECMAScript 2015 (6th Edition, ECMA-262)
ECMAScript Language: Statements and Declarations
Standard New: function*, let, for...of, yield, class
ECMAScript Latest Draft (ECMA-262)
ECMAScript Language: Statements and Declarations
Draft

浏览器兼容性

Update compatibility data on GitHub
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
Legacy generator function
Deprecated Non-standard
Chrome No support No Edge No support No Firefox No support 2 — 58 IE No support No Opera No support No Safari No support No WebView Android No support No Chrome Android No support No Firefox Android No support 4 — 58 Opera Android No support No Safari iOS No support No Samsung Internet Android No support No nodejs No support No
async function Chrome Full support 55 Edge Full support 15 Firefox Full support 52 IE No support No Opera Full support 42 Safari Full support 10.1 WebView Android Full support Yes Chrome Android Full support 55 Firefox Android Full support 52 Opera Android Full support 42 Safari iOS Full support 10.3 Samsung Internet Android Full support 6.0 nodejs Full support 7.6.0
Full support 7.6.0
Full support 7.0.0
Disabled
Disabled From version 7.0.0: this feature is behind the --harmony runtime flag.
block Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 11 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
break Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
class Chrome Full support 49
Notes
Full support 49
Notes
Notes From Chrome 42 to 48 strict mode is required. Non-strict mode support can be enabled using the flag "Enable Experimental JavaScript".
Edge Full support 13 Firefox Full support 45 IE No support No Opera Full support 36 Safari Full support 10.1 WebView Android Full support 49
Notes
Full support 49
Notes
Notes From Chrome 42 to 48 strict mode is required. Non-strict mode support can be enabled using the flag "Enable Experimental JavaScript".
Chrome Android Full support 49
Notes
Full support 49
Notes
Notes From Chrome 42 to 48 strict mode is required. Non-strict mode support can be enabled using the flag "Enable Experimental JavaScript".
Firefox Android Full support 45 Opera Android ? Safari iOS Full support 10.3 Samsung Internet Android Full support 4.0
Notes
Full support 4.0
Notes
Notes From Chrome 42 to 48 strict mode is required. Non-strict mode support can be enabled using the flag "Enable Experimental JavaScript".
nodejs Full support 6.0.0
const Chrome Full support 21 Edge Full support 12 Firefox Full support 36
Notes
Full support 36
Notes
Notes Prior to Firefox 13, const is implemented, but re-assignment is not failing.
Notes Prior to Firefox 46, a TypeError was thrown on redeclaration instead of a SyntaxError.
IE Full support 11 Opera Full support Yes Safari Full support 5.1 WebView Android Full support Yes Chrome Android Full support 25 Firefox Android Full support 36
Notes
Full support 36
Notes
Notes Prior to Firefox 13, const is implemented, but re-assignment is not failing.
Notes Prior to Firefox 46, a TypeError was thrown on redeclaration instead of a SyntaxError.
Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support Yes nodejs Full support Yes
continue Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
debugger Chrome Full support 5 Edge Full support 12 Firefox Full support 1 IE Full support 4 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
do...while Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 4 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
Empty statement (;) Chrome Full support 3 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
export Chrome Full support 61 Edge Full support 16
Full support 16
Full support 15
Disabled
Disabled From version 15: this feature is behind the Experimental JavaScript Features preference.
Firefox Full support 60
Full support 60
No support 54 — 60
Disabled
Disabled From version 54 until version 60 (exclusive): this feature is behind the dom.moduleScripts.enabled preference. To change preferences in Firefox, visit about:config.
IE No support No Opera Full support 47 Safari Full support 10.1 WebView Android No support No Chrome Android Full support 61 Firefox Android Full support 60
Full support 60
No support 54 — 60
Disabled
Disabled From version 54 until version 60 (exclusive): this feature is behind the dom.moduleScripts.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android Full support 44 Safari iOS Full support 10.3 Samsung Internet Android Full support 8.0 nodejs ?
for Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
for await...of
Experimental
Chrome Full support 63 Edge No support No Firefox Full support 57 IE No support No Opera Full support 50 Safari Full support 11 WebView Android Full support 63 Chrome Android Full support 63 Firefox Android Full support 57 Opera Android Full support 46 Safari iOS ? Samsung Internet Android Full support 8.0 nodejs Full support 10.0.0
Full support 10.0.0
No support 8.10.0 — 10.0.0
Disabled
Disabled From version 8.10.0 until version 10.0.0 (exclusive): this feature is behind the --harmony-async-iteration runtime flag.
for each...in
Deprecated Non-standard
Chrome No support No Edge No support No Firefox No support 1.5 — 57 IE No support No Opera No support No Safari No support No WebView Android No support No Chrome Android No support No Firefox Android No support 4 — 57 Opera Android No support No Safari iOS No support No Samsung Internet Android No support No nodejs No support No
for...in Chrome Full support 49 Edge Full support 12 Firefox Full support 1 IE Full support 6 Opera Full support 36 Safari Full support Yes WebView Android Full support 49 Chrome Android Full support 49 Firefox Android Full support 4 Opera Android Full support 36 Safari iOS Full support Yes Samsung Internet Android Full support 5.0 nodejs Full support Yes
for...of Chrome Full support 38 Edge Full support 12 Firefox Full support 13
Notes
Full support 13
Notes
Notes Prior to Firefox 51, using the for...of loop construct with the const keyword threw a SyntaxError ("missing = in const declaration").
IE No support No Opera Full support 25 Safari Full support 8 WebView Android Full support 38 Chrome Android Full support 38 Firefox Android Full support 14
Notes
Full support 14
Notes
Notes Prior to Firefox 51, using the for...of loop construct with the const keyword threw a SyntaxError ("missing = in const declaration").
Opera Android Full support 25 Safari iOS Full support 8 Samsung Internet Android Full support Yes nodejs Full support Yes
function Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
function* Chrome Full support 39 Edge Full support 13 Firefox Full support 26 IE No support No Opera Full support 26 Safari Full support 10 WebView Android Full support Yes Chrome Android Full support 39 Firefox Android Full support 26 Opera Android Full support Yes Safari iOS Full support 10 Samsung Internet Android Full support 4.0 nodejs Full support 4.0.0
Full support 4.0.0
Full support 0.12
Disabled
Disabled From version 0.12: this feature is behind the --harmony runtime flag.
if...else Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
import Chrome Full support 61 Edge Full support 16
Full support 16
Full support 15
Disabled
Disabled From version 15: this feature is behind the Experimental JavaScript Features preference.
Firefox Full support 60
Full support 60
No support 54 — 60
Disabled
Disabled From version 54 until version 60 (exclusive): this feature is behind the dom.moduleScripts.enabled preference. To change preferences in Firefox, visit about:config.
IE No support No Opera Full support 47 Safari Full support 10.1 WebView Android Full support 61 Chrome Android Full support 61 Firefox Android Full support 60
Full support 60
No support 54 — 60
Disabled
Disabled From version 54 until version 60 (exclusive): this feature is behind the dom.moduleScripts.enabled preference. To change preferences in Firefox, visit about:config.
Opera Android Full support 44 Safari iOS Full support 10.3 Samsung Internet Android Full support 8.0 nodejs Full support 8.5.0
Notes Disabled
Full support 8.5.0
Notes Disabled
Notes files must have suffix .mjs, not .js
Disabled From version 8.5.0: this feature is behind the --experimental-modules runtime flag.
import.meta Chrome Full support 64 Edge No support No Firefox Full support 62 IE No support No Opera Full support 51 Safari Full support 11.1 WebView Android Full support 64 Chrome Android Full support 64 Firefox Android Full support 62 Opera Android Full support 47 Safari iOS Full support 12 Samsung Internet Android Full support 9.0 nodejs ?
label Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 4 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
let Chrome Full support 49
Full support 49
No support 48 — 49
Notes Disabled
Notes Support outside of strict mode.
Disabled From version 48 until version 49 (exclusive): this feature is behind the Enable Experimental JavaScript Features preference. To change preferences in Chrome, visit chrome://flags.
No support 41 — 49
Notes
Notes Strict mode is required.
Edge Full support 14
Full support 14
No support 12 — 14
Notes
Notes In Edge 12 and 13, let within a for loop initializer does not create a separate variable for each loop iteration as defined by ES2015. Instead, it behaves as though the loop were wrapped in a scoping block with the let immediately before the loop.
Firefox Full support 44
Notes
Full support 44
Notes
Notes Prior to Firefox 44, let is only available to code blocks in HTML wrapped in a <script type="application/javascript;version=1.7"> block (or higher version) and has different semantics (e.g. no temporal dead zone).
Notes Prior to Firefox 46, a TypeError is thrown on redeclaration instead of a SyntaxError.
Notes Firefox 54 adds support of let in workers.
IE Partial support 11
Notes
Partial support 11
Notes
Notes In Internet Explorer, let within a for loop initializer does not create a separate variable for each loop iteration as defined by ES2015. Instead, it behaves as though the loop were wrapped in a scoping block with the let immediately before the loop.
Opera Full support 17 Safari Full support 10 WebView Android Full support 49
Full support 49
No support 41 — 49
Notes
Notes Strict mode is required.
Chrome Android Full support 49
Full support 49
No support 48 — 49
Notes Disabled
Notes Support outside of strict mode.
Disabled From version 48 until version 49 (exclusive): this feature is behind the Enable Experimental JavaScript Features preference. To change preferences in Chrome, visit chrome://flags.
No support 41 — 49
Notes
Notes Strict mode is required.
Firefox Android Full support 44
Notes
Full support 44
Notes
Notes Prior to Firefox 44, let is only available to code blocks in HTML wrapped in a <script type="application/javascript;version=1.7"> block (or higher version) and has different semantics (e.g. no temporal dead zone).
Notes Prior to Firefox 46, a TypeError is thrown on redeclaration instead of a SyntaxError.
Notes Firefox 54 adds support of let in workers.
Opera Android Full support 18 Safari iOS Full support 10 Samsung Internet Android Full support 5.0
Full support 5.0
No support 4.0 — 5.0
Notes
Notes Strict mode is required.
nodejs Full support 6.0.0
return Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
switch Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 4 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
throw Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 5 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
try...catch Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 5 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
var Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
while Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes
with
Deprecated
Chrome Full support 1 Edge Full support 12 Firefox Full support 1 IE Full support 3 Opera Full support Yes Safari Full support Yes WebView Android Full support 1 Chrome Android Full support 18 Firefox Android Full support 4 Opera Android Full support Yes Safari iOS Full support Yes Samsung Internet Android Full support 1.0 nodejs Full support Yes

Legend

Full support  
Full support
Partial support  
Partial support
No support  
No support
Compatibility unknown  
Compatibility unknown
Experimental. Expect behavior to change in the future.
Experimental. Expect behavior to change in the future.
Non-standard. Expect poor cross-browser support.
Non-standard. Expect poor cross-browser support.
Deprecated. Not for use in new websites.
Deprecated. Not for use in new websites.
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

参见