Ad

Ad

Saturday, 3 May 2014

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 JavaScript 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 »


Semicolon ;

Semicolon separates JavaScript statements.
Normally you add a semicolon at the end of each executable statement.
Using semicolons also makes it possible to write many statements on one line.
Writing:

a = 5;
b = 6;
c = a + b;
Is the same as writing:
a = 5; b = 6; c = a + b;

Try it Yourself »

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


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 »


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 »
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:
StatementDescription
breakTerminates a switch or a loop.  
catchMarks the block of statements to be executed when an error occurs in a try block.
continueJumps out of a loop and starts at the top.
do ... whileExecutes a block of statements and repeats the block while a condition is true.
forMarks a block of statements to be executed as long as a condition is true.
for ... inMarks a block of statements to be executed for each element of an object (or array).
functionDeclares a function.
if ... elseMarks a block of statements to be executed depending on a condition.
returnExits a function.
switchMarks a block of statements to be executed depending on different cases.
throwThrows (generates) an error.
tryImplements error handling to a block of statements.
varDeclares a variable.
whileMarks a block of statements to be executed while a condition is true.
There is a complete list of reserved words, in a later chapter of this tutorial.

White Space in JavaScript Statements

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"


Breaking a JavaScript Statement

You can break up a code line within a text string with a backslash:
var text = "Hello \
World!";
However, you cannot break up a code line like this:
var text = \ "Hello World!";

No comments:

Post a Comment