This statement tells the browser to write “Hello Dolly.” inside an HTML element with id=”demo”:
The statements are executed, one by one, in the same order as they are written.
In this example x, y, and z are given values, and finally z is displayed:
x = 5;
y = 6;
z = x + y;
document.getElementById(“demo”).innerHTML = z;
Add a semicolon at the end of each executable statement:
a = 5;
b = 6;
c = a + b;
When separated by semicolons, multiple statements on one line are allowed:
On the web, you might see examples without semicolons.
Ending statements with semicolon is not required, but highly recommended.
The following lines are equivalent:
A good practice is to put spaces around operators ( = + – * / ):
For best readability, programmers often like to avoid code lines longer than 80 characters.
The purpose of code blocks is to define statements to be executed together.
document.getElementById(“demo1”).innerHTML = “Hello Dolly!”;
document.getElementById(“demo2”).innerHTML = “How are you?”;
In this tutorial we use 4 spaces of indentation for code blocks.
You will learn more about functions later in this tutorial.
Here is a list of some of the keywords you will learn about in this tutorial:
|break||Terminates a switch or a loop|
|continue||Jumps out of a loop and starts at the top|
|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|
|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|
|try … catch||Implements error handling to a block of statements|
|var||Declares a variable|