Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

JavaScript Statements

JavaScript Statements


JavaScript statements are commands issued to the browser. The purpose of a statement is to tell the browser what to do.


JavaScript Statements

JavaScript statements are commands sent 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':

Example

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

"

Hello Dolly

"

;

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 a single line.

Example:

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 »

| lamp | You may also see examples without semicolons.
In JavaScript, ending a statement with a semicolon is optional. | | --- | --- |


JavaScript Code

JavaScript code is a sequence of JavaScript statements.

The browser executes each statement in the order it is written.

This example outputs a heading and two paragraphs to the web page:

Example

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

"

Hello Dolly

"

;

document

.

getElementById

(

"

myDIV

"

)

.

innerHTML

=

"

How have you been lately?

"

;

Try it »


JavaScript code blocks

JavaScript statements can be grouped together in blocks.

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 web page:

Example

function

myFunction

(

)

{

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

"

Hello Dolly

"

;

document

.

getElementById

(

"

myDIV

"

)

.

innerHTML

=

"

How have you been lately?

"

;

}

Try it »

You will learn more about functions in later chapters.


JavaScript Statement Identifiers

JavaScript statements usually begin with a statement identifier and execute the 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 runs the catch block when an error occurs in the try block.
continue Skip one iteration of a loop.
do ... while Execute a block of statements, and continue executing that block while the condition is true.
for While the condition is true, a block of code can be executed a specified number of times.
for ... in Used to iterate over the properties of an array or object (loop over the properties of an array or object).
function Defining 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 Implement error handling, used together with catch.
var Declare a variable.
while When the condition is true, execute the block of statements.

Space

JavaScript ignores extra spaces. You can add spaces to your script to improve readability. The following two lines of code are equivalent:



var person="runoob";

var person = "runoob";


Wrapping lines of code

You can use a backslash in a text string to wrap a line of code. The following example displays correctly:

Example

document

.

write

(

"

Hello \ world!

"

)

;

Try it »

However, you cannot do it like this:

document.write \ 

("你好世界!");

**Note:** JavaScript is a scripting language, and the browser executes the script code line by line as it reads it. In traditional programming, all code is compiled before execution.