Like other programming languages, JavaScript also has a bunch of statements and these follow a syntax. These statements consist of expressions, operators, values, keywords, and comments. A semicolon separates a statement from another. Suppose,
a=2;
b=5;
num=a+b;
These all are statements. In Javascript white spaces are ignored but if you want to add one or more white spaces for enhancing the readability of the code then it is totally your choice. It is highly recommended to add white spaces between operators (=, +, -, /). According to their usage Javascript statements are classified into different categories. Here we have listed different types of JavaScript statements for you.
1. If…else
The if/else statements are referred to as conditional statements which are used to execute a block of code if the conditions specified in that particular block are true.
Example:
if (x > 0) {
result = 'positive';
}
else {
result = 'Not positive';
}
console.log(x);
Output:
2. For
The for statement is used to execute a specified set of instructions multiple times.
Example:
document.write(i);
document.write("<br />");
}
Output:
1
2
3
3. For…in
The for…in statements are used to execute a loop over the properties of an object. These statements are a unique version of the for statements.
Example:
let text = "";
for (let x in person) {
text += person[x] + " ";
}
Output:
4. Continue
A continue statement is used inside loops to terminate an iteration and the control is shifted to the beginning of a loop for the next iteration.
Example:
for (let i = 1; i < 5; i++) {
if (i === 3) {
continue;
}
n = n + i;
}
console.log(n);
Output:
1
2
4
5
5. Switch
A switch statement is also referred to as a conditional statement. These statements perform certain actions that are based on specified conditions.
Example:
var fruits = document.getElementById("myInput").value;
switch(fruits) {
case "Banana":
text = "Banana is good!";
break;
case "Orange":
text = "I am not a fan of orange.";
break;
case "Apple":
text = "How you like them apples?";
break;
default:
text = "I have never heard of that fruit...";
}
Output:
Let’s say you enter ‘mango’’. The output will be:
6. Break
A break statement is used to end a loop and other statements like switch and label. The control is shifted to the statement that comes right after the statement that just has been terminated.
Example:
while (n < 5) {
if (n === 2) {
break;
}
n = n + 1;
}
console.log(i);
Output:
7. Block
A block statement is used to group multiple statements. A block is defined by placing curly brackets ‘{ }’ at the beginning and the end of the group of statements(block).
Example:
let b= 1;
if (true) {
var a = 2;
let b= 2;
}
console.log(x);
console.log(y);
Output:
1
8. Try…catch
These statements are an exception that marks a block of code to try and specify a response.
Example:
alert('Start of try runs');
abcdef;
alert('End of try (never reached)');
}
catch (err) {
alert(`Error has occurred!`);
}
Output:
9. Throw
A throw statement is a special type of user-defined statement that is used to create custom errors.
Example:
if (typeof x !== "Number" || isNaN(x)) {
throw "sorry, x is not a number";
}
console.log((x *= 2));
}
try {
let x = "X";
doubleNumber(x);
} catch (e) {
console.error(e);
}
Output:
10. Empty
An empty statement in JavaScript is used to specify that no statements will be executed. These statements are specified using just a semicolon (;).
Example:
for (let i = 0; i < array1.length; array1[i++] = 0);
console.log(array1);
Output:
Conclusion
Statements are fundamental blocks in a computer program that are used to specify instructions. JavaScript also makes use of multiple statements, each having a separate function and purpose. These statements define the basic rules of writing a JavaScript program. This tutorial enlightens its readers with various JavaScript statements along with suitable examples.