X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR BOOTSTRAP XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

JavaScript Statements


In HTML, JavaScript statements are "command lines" executed by the web browser.


JavaScript Statements

In HTML, JavaScript statements are "commands" to the browser.

The purpose, of the statements, is to tell the browser what to do.

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

Example

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

Try it Yourself »


JavaScript Code

JavaScript code (or just JavaScript) is a sequence of JavaScript statements.

Each statement is executed by the browser in the sequence they are written.

This example will manipulate two different HTML elements:

Example

document.getElementById("demo").innerHTML = "Hello Dolly.";
document.getElementById("myDiv").innerHTML = "How are you?";

Try it Yourself »


Semicolon ;

Semicolon separates JavaScript statements.

Normally you add a semicolon at the end of each executable statement:

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

Try it Yourself »

Multiple statements on one line are allowed:

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

Try it Yourself »

Note You might see examples without semicolons.
Ending statements with semicolon is optional in JavaScript.


JavaScript Code Blocks

JavaScript statements can be grouped together in blocks.

Blocks start with a left curly bracket, and end with a right curly bracket.

The purpose of a block is to make the sequence of statements execute together.

A good example of statements grouped together in blocks, are in JavaScript functions.

This example will run a function that will manipulate two HTML elements:

Example

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

Try it Yourself »

Note In this tutorial we use 4 spaces of indentation for code blocks.
You will learn much more about functions later in this tutorial.


JavaScript Statement Identifiers

JavaScript statements often start with a statement identifier to identify the JavaScript action to be performed.

Statement identifiers are reserved words and cannot be used as variable names (or any other things).

Here is a list of some of the JavaScript statements (reserved words) you will learn about in this tutorial:

Statement Description
break Terminates a switch or a loop
catch Marks the block of statements to be executed when an error occurs in a try block
continue Jumps out of a loop and starts at the top
debugger Stops the execution of JavaScript, and calls (if available) the debugging function
do ... while Executes a block of statements, and repeats the block, while a condition is true
for Marks a block of statements to be executed, as long as a condition is true
for ... in Marks a block of statements to be executed, for each element of an object (or array)
function Declares a function
if ... else Marks a block of statements to be executed, depending on a condition
return Exits a function
switch Marks a block of statements to be executed, depending on different cases
throw Throws (generates) an error
try Implements error handling to a block of statements
var Declares a variable
while Marks a block of statements to be executed, while a condition is true


JavaScript White Space

JavaScript ignores extra spaces. You can add white space to your script to make it more readable.

The following lines are equivalent:

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


JavaScript Line Length and Line Breaks

For best readability, programmers often like to avoid lines longer than 80 characters.

If a JavaScript statement does not fit on one line, the best place to break it, is after an operator or a comma.

Example

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

Try it Yourself »


JavaScript Line Break in a String

You can break up a code line within a text string with a backslash:

Example

document.getElementById("demo").innerHTML = "Hello \
    Dolly!";

Try it Yourself »

However, you cannot break up a code line like this:

Example

document.getElementById("demo").innerHTML = \
    "Hello Dolly!";

Try it Yourself »


Try it Yourself Summary

JavaScript statements are commands to the browser

JavaScript code is a sequence of statements

Statements are separated with semicolon

Multiple statement on one line is allowed

JavaScript can be grouped together in code blocks

You can break a code line after an operator or a comma.

You can break text string with a backslash.

You cannot break code with a backslash.




Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook