English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
A computer program is a list of instructions that a computer needs to execute.
In programming languages, these programming instructions are called statements.
A JavaScript program is a list of programming statements.
Statements are used in JavaScript to specify the flow of the program.
JavaScript statements are composed of the following items:Values, operators, expressions, keywords, and comments.
This statement tells the browser to write the content "Hello world" inside the HTML element with id="para":
document.getElementById("para").innerHTML = "Hello world";Test see‹/›
Most JavaScript programs contain many JavaScript statements.
Statements are executed in the order they are written.
var a, b, sum;// Statement 1 a = 20; // Statement 2 b = 30; // Statement 3 sum = a + b; // Statement 4Test see‹/›
Statements define the operations that the script will execute and how they will be executed.
And C, C ++Like in Java, simple statements in JavaScript are usually followed by a semicolon character.
But if each statement is placed on a separate line, JavaScript allows you to omit this semicolon.
var x = 20 var y = 30Test see‹/›
But when formatted in a single-line format as shown below, a semicolon must be used:
var x = 20; var y = 30;Test see‹/›
Although it is feasible to end statements without a semicolon (;),But it is still strongly recommended to end with a semicolon.
Using a semicolon is a good programming habit.
JavaScript ignores spaces, tabs, and newline characters that appear in the JavaScript program.
You can add spaces in the script to make it more readable.
The following two lines are equivalent:
var greet = "Hello world"; var greet = "Hello world";
It is generally recommended to place spaces around the operator ( +-* /)spaces around, to enhance readability:
var sum = a + b ;
To ensure the best readability, it is advisable to avoid code lines that exceed80 characters.
If the JavaScript statement does not fit on one line, the best place to break the line is at the operator ( +-* /)after:
document.getElementById("para").innerHTML = "20+3The sum of 0 is " + sum;Test see‹/›
JavaScript statements can be grouped together within a code block {...}.
The purpose of the code block is to define the statements to be executed together.
InIn JavaScript functionsYou will find that statements grouped together in a block are placed at one place.
function myFunc() { document.getElementById("p1").innerHTML = "Hey, there!"; document.getElementById("p2").innerHTML = "How are you doing?"; }Test see‹/›
JavaScript statements usually end withKeywordStarts with, to identify the JavaScript operation to be executed.
Below is a list of some keywords you will learn about in this tutorial:
Keyword | Description |
---|---|
var | Declare a variable, and you can choose to initialize it with a value |
let | Declare a block {} scope local variable, and you can choose to initialize it with a value |
const | Declare a read-only named constant |
break | Terminate the current loop, switch, or labeled statement, and transfer program control to the statement after the termination statement |
continue | Terminate the execution of the statement in the current loop or marked loop at the current iteration, and continue the loop in the next iteration |
if...else | If the specified condition is true, execute a statement. If the condition is false, you can execute another statement |
switch | Evaluate the expression to make the value of the expression match the case clause, and execute the statement related to the case |
throw | Throw a user-defined exception |
try...catch | Mark the statement block to be attempted and specify the response (if an exception occurs) |
while | Create a loop that executes the specified statement as long as the value of the specified condition is true. The condition is checked before executing the statement |
do...while | Create a loop that executes the specified statement until the value of the test condition is false. After executing the statement, evaluate the condition, causing the specified statement to be executed at least once |
for | Create a loop consisting of three optional expressions, enclosed in parentheses and separated by semicolons, followed by the statements to be executed in the loop |
for...in | Iterate over the enumerable properties of an object in any order. For each different property, you can execute a statement |
for...of | Iterate over iterable objects (including arrays, array-like objects, iterators, and generators), call custom iteration hooks, and execute the specified statements for each different property value |