JavaScript Statement
A command issued by a JavaScript statement to the browser. The purpose of the statement is to tell the browser what to do.
JavaScript Statement
JavaScript statements are commands sent to the browser.
The purpose of these commands is to tell the browser what to do.
The following JavaScript statement outputs the text "Hello Jake" to the HTML element with id="demo":
Example
document.getElementById("demo").innerHTML = "Hello Jake";
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 on one line.
Examples:
a = 5; b = 6; c = a + b;
The above example can also be written like this:
a = 5; b = 6; c = a + b;
![]() | You may also see cases without semicolons. In JavaScript, ending a statement with a semicolon is optional. |
---|
JavaScript Codes
JavaScript code is a sequence of JavaScript statements.
The browser executes each statement in sequence in the order in which it was written.
This example outputs a title and two paragraphs to the web page:
Example
document.getElementById("demo").innerHTML="Hello Jake"; document.getElementById("myDIV").innerHTML="How are you doing?";
JavaScript Code Block
JavaScript can be combined in batches.
The code block starts with an opening curly brace and ends with a closing curly brace.
The function of a code block is to execute a sequence of statements together.
This example outputs a title and two paragraphs to the web page:
Example
function myFunction() { document.getElementById("demo").innerHTML="Hello Jake"; document.getElementById("myDIV").innerHTML="How are you doing?"; }
You will learn more about functions in later chapters.
JavaScript Statement Identifiers
JavaScript statements usually a statement identifier for the beginning, and execute the statement.
The statement identifier is a reserved keyword and cannot be used as a variable name.
The following table lists JavaScript statement identifiers (keywords):
Statement | Description |
---|---|
break | Used to jump out of the loop. |
catch | Statement block, the catch statement block is executed when an exception occurs in the try statement block. |
continue | Skip an iteration in the loop. |
do ... while | Execute a statement block and continue to execute the statement block when the conditional statement is true. |
for | When the conditional statement is true, the code block can be executed a specified number of times. |
for ... in | It is used to traverse the properties of arrays or objects (loop operations on the properties of arrays or objects). |
function | Define a function |
if ... else | Used to perform different actions based on different conditions. |
return | Exit function |
switch | Used to perform different actions based on different conditions. |
throw | Throw (generate) an exception. |
try | Implement exception handling and use it with catch. |
var | Declare a variable. |
while | When the conditional statement is true, the statement block is executed. |
Space
JavaScript ignores extra spaces. You can add spaces to the script to improve its readability. The following two lines of code are equivalent:
var person="tutorialfish";
var person = "tutorialfish";
Wrap Lines of Codes
You can use backslashes in text strings to wrap lines of code. The following example will show correctly:
Example
document.write("Hello \
world!");
However, you cannot wrap lines like this:
document.write \ ("Hello World!");
Tip: JavaScript is a scripting language. When reading the code, the browser will execute the script code line by line. For traditional programming, all codes are compiled before execution.