English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Statements and Declarations

A JavaScript application is composed of statements with proper grammar.

JavaScript statements are "instructions" that web browsers need to "execute".

A statement can span multiple lines.

If each statement is separated by a semicolon, multiple statements may appear on one line.

This statement tells the browser to write "Hello world" inside the HTML element with id="para":

document.getElementById("para").innerHTML = "Hello world";
Test to see‹/›

You can find in ourIn the JavaScript statements tutorialLearn more about statements.

The following is a list of JavaScript statements and declarations listed by category:

Variable declaration

Declaration
Description
varDeclare a variable, which can optionally be initialized with a value
letDeclare a block {} scoped local variable, which can optionally be initialized with a value
constDeclare a read-only named constant

Flow control

StatementDescription
breakTerminate the current loop, switch, or label statement, and pass control to the statement after the terminating statement.
continueTerminate the execution of a statement in the current loop or the current iteration of a labeled loop, and continue executing the loop in the next iteration.
if...elseIf the specified condition is true, execute a statement. If the condition is false, another statement can be executed.
switchCalculate the expression so that the value of the expression matches the case clause, and execute the statement related to the case
throwThrow a user-defined exception
try...catchMark the statement block to be attempted and specify the response when an exception is thrown

Iteration Statement

StatementDescription
while Create a loop that executes the specified statement as long as the computed result of the test condition is true. The condition is checked before the statement is executed
do...whileCreate a loop that executes the specified statement until the value of the test condition is false. The statement is executed first, and then the condition is evaluated, so the specified statement is executed at least once
forCreate a loop consisting of three optional expressions, enclosed in parentheses and separated by semicolons, followed by the statement to be executed in the loop
for...inTraverse the enumerable properties of an object in arbitrary order. For each different property, you can execute a statement
for...ofIterate over iterable objects (including arrays, array-like objects, iterators, and generators), call custom iteration hooks, and execute statements for each different property value