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:
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 2 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:
|var||Declares a variable|
|let||Declares a block variable|
|const||Declares a block constant|
|if||Marks a block of statements to be executed on a condition|
|switch||Marks a block of statements to be executed in different cases|
|for||Marks a block of statements to be executed in a loop|
|function||Declares a function|
|return||Exits a function|
|try||Implements error handling to a block of statements|