JavaScript Statements
JavaScript statements are commands sent to the browser. The purpose of statements is to tell the browser what to do.
JavaScript Statements
JavaScript statements are commands issued to the browser.
These commands tell the browser what to do.
The following JavaScript statement outputs the text "Hello Dolly" to the HTML element with id="demo":
Semicolon ;
Semicolons are used to separate JavaScript statements.
Usually we add a semicolon at the end of each executable statement.
Another use of semicolons is to write multiple statements in one line.
The above example can also be written like this:
Try it Yourself »
| You may also see cases without semicolons. In JavaScript, ending statements with semicolons is optional. |
JavaScript Code
JavaScript code is a sequence of JavaScript statements.
The browser executes each statement in the order they are written.
This example outputs a heading and two paragraphs to the webpage:
Examples
Try it Yourself »
JavaScript Code Blocks
JavaScript statements can be grouped together in batches.
A code block starts with a left curly brace and ends with a right curly brace.
The purpose of a code block is to execute a sequence of statements together.
This example outputs a heading and two paragraphs to the webpage:
Examples
Try it Yourself »
You will learn more about functions in a later chapter.
JavaScript Statement Identifiers
JavaScript statements often start with astatement identifierand execute that statement.
Statement identifiers are reserved keywords and cannot be used as variable names.
The following table lists JavaScript statement identifiers (keywords):
| Statement | Description |
|---|---|
| break | Used to break out of a loop. |
| catch | A statement block that executes the catch statement block when an error occurs in the try statement block. |
| continue | Skips one iteration in a loop. |
| do ... while | Executes a statement block, and continues executing it while the condition is true. |
| for | When the condition is true, the code block can be executed a specified number of times. |
| for ... in | Used to iterate over the properties of an array or object (to loop over the properties of an array or object). |
| function | Define a function |
| if ... else | Used to perform different actions based on different conditions. |
| return | Return a result and exit the function |
| switch | Used to perform different actions based on different conditions. |
| throw | Throw (generate) an error. |
| try | Implements error handling, used together with catch. |
| var | Declare a variable. |
| while | When the condition is true, execute the statement block. |
Spaces
JavaScript ignores extra spaces. You can add spaces to your script to improve its readability. The following two lines of code are equivalent:
var person="example"; var person = "example";
Line Wrapping
You can use a backslash to wrap code lines within a text string. The following example will display correctly:
However, you cannot do it like this:
document.write \
("你好世界!");

Key Points:JavaScript is a scripting language. The browser executes the script code line by line as it reads the code. For traditional programming, however, all code is compiled before execution.
Other Extensions