WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

JavaScript Statements


command issued by a JavaScript statement to the browser. The purpose of the 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.

This statement tells the browser to write "Hello Dolly." inside an HTML element with id="demo":

Example

document.getElementById("demo").innerHTML = "Hello Dolly.";
Try it Yourself »

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.

Examples:

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

The above example can also be written like this:

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

Npte: You may also see cases without semicolons.
Semi-colons are optional in JavaScript.

JavaScript code

JavaScript code is a sequence of JavaScript statements.

The browser executes each statement in turn, in the order they were written.

This example outputs a title and two paragraphs to a web page:

Example

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

JavaScript code block

JavaScript can be combined in batches.

The code block begins with a left curly brace and ends with a right 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 a web page:

Example

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

JavaScript statement identifier

JavaScript statements usually begin with a statement identifier 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 is used to exit the loop.
catch statement block, which executes a catch statement block when an error occurs in the try statement block.
continue Skip an iteration in the loop.
do ... while Executes a block of statements, which continues to execute 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 is used to iterate over the attributes of an array or object (loop operations on the attributes of an array or object).
function Define a function
if ... else is used to perform different actions based on different conditions.
return Exit function
switch is used to perform different actions based on different conditions.
throw Throws (generates) an error.
try Implement error handling and use with catch.
var Declares a variable.
while When the conditional statement is true, execute the statement block.

space

JavaScript ignores extra spaces. You can add spaces to the script to make it more readable. The following two lines of code are equivalent:

var person = "Hege";
var person = "Hege";


Wrap lines of code

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

document.write ("Hello \
World! ");

However, you cannot break lines like this:

document.write \
("Hello world!");


Did you know?

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