Statements in Javascript are the step by step instructions to be executed by the computer. A JavaScript program is built up of its programming statements.
Javascript statements may composed of :
Variable
Value
Operators
Expressions
Keywords and
Comments
The statements are inserted in HTML document between <script> and </script> tags and run by web browsers.
see an easy example:
<html>
<body>
<h2>JavaScript Statements</h2>
<p>JavaScript Statements are the step by step instructions to be executed by computer. </p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = "Hello World.";
</script>
</body>
</html>
Output |
A JavaScript program may consists of many statements and these statements are executed one by one in the order, in which they are written. Each statement separates from each other by ;(semicolon).
For ex:
<html>
<body>
<h2>JavaScript Statements</h2>
<p>JavaScript statements are instructions executed one by one.</p>
<p id="demo"></p>
<script>
var a, b, c; //declair three variables named a, b and c
a = 5; //assign the value 5 to a
b = 6; //assign the value 6 to b
c = a + b; //assign the value of a+b to c
document.getElementById("demo").innerHTML = c;
</script>
</body>
</html>
Now try it yourself in below editor
Output |
In above editor change any value of variable a or b, and see the result. We can use 'Let' keyword to store a direct value into a variable(like c = 7 + 6). But when we store a variable into another variable (like c =a + b) then we use 'var' keyword.
We can also write multiple statements in one line separated from each other by semicolon.
like var a, b, c; a = 5; b = 6;
c = a + b;
You can try in above editor window.
Output |
JavaScript Keywords
JavaScript statement often starts with a keyword to identify the action to be performed. These keywords are reserved words for JavaScript. Let's know some most usual keywords of JavaScript:
Keyword |
Description |
let |
Assign a value
inside a variable |
var |
Assign a variable inside a variable |
const |
Assign constant number or string(value cant be changed
during execution) |
if |
Identify a block of statements to be executed on given
condition |
switch |
Identify a
block of statements to be executed in different cases |
for |
Identify 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 |