JavaScript

JavaScript Statements

Statements are the building blocks of a computer program. These are instructions given to a computer to be executed by it. For example, the statement given below is a Javascript statement that sets the value of variable x equal to 5.

let var x=5;

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,

let a,b,num;
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:

var x = 5;
if (x > 0) {
 result = 'positive';
}
else {
 result = 'Not positive';
}
console.log(x);

Output:

Positive

2. For

The for statement is used to execute a specified set of instructions multiple times.

Example:

for (let i=0; i<4; i++) {
document.write(i);
document.write("<br />");
}

Output:

0
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:

constperson = {fname:"John", lname:"Mike", age:20};
let text = "";
for (let x in person) {
  text += person[x] + " ";
}

Output:

John Mike 20

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:

let  n='';
for (let i = 1; i < 5; i++) {
  if (i === 3) {
    continue;
  }
  n = n + i;
}
console.log(n);

Output:

0
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 text;
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:

I have never heard of that fruit

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:

let n = 0;
while (n < 5) {
  if (n === 2) {
    break;
  }
  n = n + 1;
}
console.log(i);

Output:

2

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:

var a = 1;
let b= 1;

if (true) {
  var a = 2;
  let b= 2;
}
console.log(x);
console.log(y);

Output:

2
1

8. Try…catch

These statements are an exception that marks a block of code to try and specify a response.

Example:

try {
  alert('Start of try runs');  
  abcdef;
  alert('End of try (never reached)');  

}
catch (err) {
  alert(`Error has occurred!`);
}

Output:

Error has occurred!

9. Throw

A throw statement is a special type of user-defined statement that is used to create custom errors.

Example:

function doubleNumber(x) {
  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:

sorry, x is not a number

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:

const array1 = [1, 2, 3];

for (let i = 0; i < array1.length; array1[i++] = 0);

console.log(array1);

Output:

Array[0,0,0]

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.

About the author

Naima Aftab

I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.