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":


document.getElementById("demo").innerHTML = "Hello Jake";

 Try It! 

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.


a = 5;
b = 6;
c = a + b;

The above example can also be written like this:

a = 5; b = 6; c = a + b;

 Try It! 

lampYou 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:


document.getElementById("demo").innerHTML="Hello Jake";
document.getElementById("myDIV").innerHTML="How are you doing?";

 Try It! 

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:


function myFunction()
     document.getElementById("demo").innerHTML="Hello Jake";
     document.getElementById("myDIV").innerHTML="How are you doing?";

 Try It! 

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):

breakUsed to jump out of the loop.
catchStatement block, the catch statement block is executed when an exception occurs in the try statement block.
continueSkip an iteration in the loop.
do ... whileExecute a statement block and continue to execute the statement block when the conditional statement is true.
forWhen the conditional statement is true, the code block can be executed a specified number of times.
for ... inIt is used to traverse the properties of arrays or objects (loop operations on the properties of arrays or objects).
functionDefine a function
if ... elseUsed to perform different actions based on different conditions.
returnExit function
switchUsed to perform different actions based on different conditions.
throwThrow (generate) an exception.
tryImplement exception handling and use it with catch.
varDeclare a variable.
whileWhen the conditional statement is true, the statement block is executed.


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:


document.write("Hello \


 Try It! 

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.