Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Statements</h2> <p>A <b>JavaScript program</b> is a list of <b>statements</b> to be executed by a computer.</p> <p id="demo"></p> <script> let x, y, z; // Statement 1 x = 5; // Statement 2 y = 6; // Statement 3 z = x + y; // Statement 4 document.getElementById("demo").innerHTML = "The value of z is " + z + "."; </script> </body> </html>
Result:
JavaScript Statements
A JavaScript program is a list of statements to be executed by a computer.
The value of z is 11.
JavaScript Programs
A computer program is a list of “instructions” to be “executed” by a computer.
In a programming language, these programming instructions are called statements.
A JavaScript program is a list of programming statements.
In HTML, JavaScript programs are executed by the web browser.
JavaScript Statements
JavaScript statements are composed of:
Values, Operators, Expressions, Keywords, and Comments.
This statement tells the browser to write “Hello Dolly.” inside an HTML element with id=”demo”:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Statements</h2> <p>In HTML, JavaScript statements are executed by the browser.</p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "Hello Dolly."; </script> </body> </html>
Result:
JavaScript Statements
In HTML, JavaScript statements are executed by the browser.
Hello Dolly.
Most JavaScript programs contain many JavaScript statements.
The statements are executed, one by one, in the same order as they are written.
JavaScript programs (and JavaScript statements) are often called JavaScript code.
Semicolons ;
Semicolons separate JavaScript statements.
Add a semicolon at the end of each executable statement:
Examples
<!DOCTYPE html> <html> <body> <h2>JavaScript Statements</h2> <p>JavaScript statements are separated by semicolons.</p> <p id="demo1"></p> <script> let a, b, c; a = 5; b = 6; c = a + b; document.getElementById("demo1").innerHTML = c; </script> </body> </html>
Result:
JavaScript Statements
JavaScript statements are separated by semicolons.
11
When separated by semicolons, multiple statements on one line are allowed:
Examples
<!DOCTYPE html> <html> <body> <h2>JavaScript Statements</h2> <p>Multiple statements on one line are allowed.</p> <p id="demo1"></p> <script> let a, b, c; a = 5; b = 6; c = a + b; document.getElementById("demo1").innerHTML = c; </script> </body> </html>
Result:
JavaScript Statements
Multiple statements on one line are allowed.
11
JavaScript White Space
JavaScript ignores multiple spaces. You can add white space to your script to make it more readable.
The following lines are equivalent:
let person = “Hege”;
let person=”Hege”;
A good practice is to put spaces around operators ( = + – * / ):
let x = y + z;
JavaScript Line Length and Line Breaks
For best readability, programmers often like to avoid code 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:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Statements</h2> <p> The best place to break a code line is after an operator or a comma. </p> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "Hello Dolly!"; </script> </body> </html>
Result:
JavaScript Statements
The best place to break a code line is after an operator or a comma.
Hello Dolly!
JavaScript Code Blocks
JavaScript statements can be grouped together in code blocks, inside curly brackets {…}.
The purpose of code blocks is to define statements to be executed together.
One place you will find statements grouped together in blocks, is in JavaScript functions:
Example
<!DOCTYPE html> <html> <body> <h2>JavaScript Statements</h2> <p>JavaScript code blocks are written between { and }</p> <button type="button" onclick="myFunction()">Click Me!</button> <p id="demo1"></p> <p id="demo2"></p> <script> function myFunction() { document.getElementById("demo1").innerHTML = "Hello Dolly!"; document.getElementById("demo2").innerHTML = "How are you?"; } </script> </body> </html>
In this tutorial we use 2 spaces of indentation for code blocks.
You will learn more about functions later in this tutorial.
JavaScript Keywords
JavaScript statements often start with a keyword to identify the JavaScript action to be performed.
Our Reserved Words Reference lists all JavaScript keywords.
Here is a list of some of the keywords you will learn about in this tutorial:
Keyword | Description |
---|---|
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 |
JavaScript keywords are reserved words. Reserved words cannot be used as names for variables.