x = 5; // Statement 2
y = 6; // Statement 3
z = x + y; // Statement 4
A computer program is a list of “instructions” to be “executed” by a computer.
In a programming language, these programming instructions are called statements.
Values, Operators, Expressions, Keywords, and Comments.
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.
Add a semicolon at the end of each executable statement:
a = 5; // Assign the value 5 to a
b = 6; // Assign the value 6 to b
c = a + b; // Assign the sum of a and b to c
When separated by semicolons, multiple statements on one line are allowed:
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|