This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
1.4 JS Statements
var x, y, z; // Statement 1
x = 5; // Statement 2
y = 6; // Statement 3
z = x + y; // Statement 4
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":
document.getElementById("demo").innerHTML = "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:
var a, b, c; // Declare 3 variables
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:
a = 5; b = 6; c = a + b;
On the web, you might see examples without semicolons.
Ending statements with semicolon is not required, but highly recommended.
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:
var person = "Hege";
var person="Hege";
A good practice is to put spaces around operators ( = + - * / ):
var 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:
document.getElementById("demo").innerHTML =
"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:
function myFunction() {
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.
JavaScript Keywords
- JavaScript statements often start with a keyword to identify the JavaScript action to be performed.
- Here is a list of some of the keywords you will learn about in this tutorial:
Keyword |
Description |
break |
Terminates a switch or a loop |
continue |
Jumps out of a loop and starts at the top |
debugger |
Stops the execution of JavaScript, and calls (if available) the debugging function |
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 |
JavaScript keywords are reserved words. Reserved words cannot be used as names for variables.
Module 1. Introduction to JavaScript
Navigate this module
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon
Example files created in this module:
What Can JavaScript Do? part 1
What Can JavaScript Do? part 2
What Can JavaScript Do? part 3
What Can JavaScript Do? part 4
What Can JavaScript Do? part 5
What Can JavaScript Do? part 6
JavaScript in Body
Demo JavaScript in Head
Demo JavaScript in Body
Demo External JavaScript
External JavaScript part 1
External JavaScript part 2
External JavaScript part 3
My First Web Page part 1
My First Web Page part 2
My First Web Page part 3
My First Web Page part 4
My First Web Page part 5
Activate Debugging
The window.print() Method
JavaScript Statements part 1
JavaScript Statements part 2
JavaScript Statements part 3
JavaScript Statements part 4
JavaScript Statements part 5
JavaScript Statements part 6
JavaScript Numbers
JavaScript Strings
JavaScript Variables
JavaScript Operators
Assigning JavaScript Values
JavaScript Expressions part 1
JavaScript Expressions part 2
JavaScript Expressions part 3
The var Keyword Creates Variables
JavaScript Comments are NOT Executed
JavaScript is Case Sensitive
More JavaScript Comments
JavaScript Comments part 2
JavaScript Comments part 3
JavaScript Comments part 4
JavaScript Comments part 5