A computer program is a list for a computer to "run" the instructions. These instructions are referred to as statements in a programming language.
Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>A <b>Java Script program</b> is a list of <b>statements</b> to be executed by a computer.</p>
<p id="demo"></p>
<script>
var x, y, z; // Statement 1
x = 3; // Statement 2
y = 5; // Statement 3
z = x + y; // Statement 4
document.getElementById("demo").innerHTML =
"The value of z is " + z + ".";
</script>
</body>
</html>
Note: In HTML, JavaScript programs are executed by the web browser.
Output:
A Java Script program is a list of computer statements.
The declarations in Java Script include: values, operators, expressions, keywords and commentary. This statement says to the browser "Hello UK" to write "id="demo" inside a HTML element:
Example:
<!DOCTYPE html>
<html>
<body>
<h2>Java Script Statements</h2>
<p>In HTML, Java Script statements are executed by the browser.</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello UK.";
</script>
</body>
</html>
Most Java Script programs contain many Java Script statements. The declarations are performed in the same order as they are written one by one.
Note: Java Script programs (and Java Script statements) are often called Java Script code.
Output:
In HTML, Java Script statements are executed by the browser.
Semicolons separate Java Script statements. Add a semicolon at the end of each executable statement:
Example:
<!DOCTYPE html>
<html>
<body>
<h2>Java Script Statements</h2>
<p>Java Script statements are separated by semicolons.</p>
<p id="demo1"></p>
<script>
var a, b, c;
a = 5;
b = 6;
c = a + b;
document.getElementById("demo1").innerHTML = c;
</script>
</body>
</html>
Output:
Java Script statements are separated by semicolons.
When separated by semicolons, multiple statements on one line are allowed:
Example:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>Multiple statements on one line is allowed.</p>
<p id="demo1"></p>
<script>
var a, b, c;
a = 5; b = 6; c = a + b;
document.getElementById("demo1").innerHTML = c;
</script>
</body>
</html>
Note: Without semicolons, we can see examples in the web. Semicolon termination statements are not necessary but strongly recommended.
Output:
Multiple statements on one line is allowed.
Java Script ignores multiple spaces. To make your script more readable, we can add white space to it.
The following lines are equivalent:
Example:
var person = "Hege";
var person="Hege";
A good practice is to put spaces around operators ( = + - * / ):
var x = y + z;
For best readability, programmers often like to avoid code lines longer than 80 characters. If a Java Script statement does not fit on one line, the best place to break it is after an operator:
Example:
<!DOCTYPE html>
<html>
<body>
<h2>Java Script Statements</h2>
<p>Java Script 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 UK!";
document.getElementById("demo2").innerHTML = "We Love Uttarakhand";
}
</script>
</body>
</html>
Output:
Java Script code blocks are written between { and }
Note: In this tutorial we use 4 spaces of indentation for code blocks.
Java Script statements often start with a keyword to identify the Java Script action to be performed. Here is a list of some of the keywords we 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 |
Note: JavaScript keywords are reserved words. Unable to use reserved words as variable names.