Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Types of Control Statements in JS
2.
Examples of Control Statements
2.1.
Conditional Statements
2.2.
If Statment
2.3.
Syntax:
2.4.
If-Else Statement
2.5.
Syntax:
2.6.
Switch Statement
2.7.
Syntax:
2.8.
Iterative Statement
2.9.
While Statement
2.10.
Syntax:
2.11.
Do-While Statement
2.12.
Syntax:
2.13.
For Statement
2.14.
Syntax:
3.
Frequently Asked Questions
3.1.
Why do we use control statements?
3.2.
What are the 5 JavaScript statements?
3.3.
What are control statement and loop?
3.4.
What are the 3 types of functions in JavaScript?
4.
Conclusion
Last Updated: Jun 11, 2024
Easy

Control Statements in Javascript

Control statements are like normal statements in any programming language but with the power to decide which statement will execute at which time. Examples of Control statements can be If-Else statements or switch statements. 

control statements in javascript

In any program, a typical statement aims to execute a command or, in simple words, do something. Control Statements are similar to other statements. But they are different at a certain angle. They have the ability to determine which of the other statements should be executed and at what time it should be executed.

Control statements are intended to allow users to write scripts that determine which lines of code get evaluated and how many times they get evaluated. Control statements are classified into two types.

Types of Control Statements in JS

There are basically two types of control statements in Javascript. Namely, Conditional Statements and Iterative Statements (in simple words, Loops).

  • Conditional Statements: These are the ones that make a decision. There is an expression that gets passed, and then the conditional statement decides on it. It is either YES or NO.
  • Iterative Statements (Loop):  This is a type of statement that keeps repeating itself till a condition is satisfied. In simple words, if we have an expression, until and unless it gets satisfied, the statement will keep repeating itself.

Examples of Control Statements

Now let us take a look at some examples and try to understand more about control statements.

Conditional Statements

This is where we decide the flow of a program's execution. Based on the outcome, Conditional Statements determine the next step. The outcome of a conditional assertion can be True, or it can be false. If the condition comes out to be true, the programme goes to the next step. On the other hand, if the condition comes out to be false, the programme goes out to a different. Unlike Loop statements, these statements get executed only once.

Below we can see the different types of  Conditional Statements.

If Statment

As the name suggests, we use the If Statement if we want to check for a specific condition. When we are using the IF condition, the inner code block gets executed only when the given condition is satisfied.

Syntax:

if (condition_is_given_here) {
// If the condition is met, the code block will get executed.
}


If-Else Statement

The If-Else Statement is an extended version of the If statement. We use this one when we want to check a condition we want and another one if the evaluation turns out otherwise.

Syntax:

if (condition_is_given_here) {
// If the condition is met, this code block will get executed.
}
else {
/*If the condition is not met and turns out to be false, this code block will get executed. */
}


As we can see, the first block of code gets executed when the condition in IF-ELSE is satisfied. The second block of code gets executed when the condition does not get satisfied.


Switch Statement

A switch statement is similar to an IF statement. It is used when the user needs to execute one code block execution from their own choices out of several code block execution options. It is based on the outcome of the expression given. Switch statements hold an expression that is compared to the values of the following cases. If a match is found, the code associated with that case is executed.

Syntax:

switch (expression) {
case first:
// Here, we add the code block that is to be executed
Break; 
// Break creates a separation between each case
case second:
// Here, we add the code block that is to be executed
Break;
case third:
// Here, we add the code block that is to be executed
Break;
default:
// Here, we add the default code block that is to be executed. It happens if none of the above cases is executed.
}


At the beginning of the code, an expression gets checked and compared with the cases given. If the given expression matches the first case, the code block present within the case gets executed. The same is true for the second case and the third case. However, when the expression passed matches none of the cases given, the code enters the default case and executes the code that is under the default case.

Now that we've covered conditional statements, it is time to move on to the second category, iterative statements.

Iterative Statement

Looping is a strong feature in any programming language. This is for repeatedly executing a sequence of instructions while the expression passed is met. A simple example would be to print "Hello Ninjas" ten times. Now, typing the same print statement with "Hello Ninjas" ten times in a row will take time. As an influence, it will also affect the execution time. This is the place where looping comes to play. 

We should know that iterative statements are divided into three types: WHILE, DO-WHILE, and FOR. Let's break each down with syntax and take a look at them.

While Statement

One of the control flow statements is the. When the condition is met, they run a code block. However, unlike the IF statement, while keeps repeating itself until the condition is met. The difference between IF and while is that IF executes code when the condition is met. Whereas while statement keeps repeating itself till the condition is met.

Syntax:

while (the_condition_is_put_here)
{
// The code block is executed when the conditions are met
}


Do-While Statement

We can call the Do-While Statement a sibling of the While Statement. Similar to a while loop, but with the addition of a condition at the end of the loop. DO-WHILE, also known as an Exit Control Loop, performs the code and then checks for the condition.

Syntax:

while
{
// Code block gets executed till the condition is satisfied
} (we_add_the_conditon_here)


The loop will get repeated if the condition at the end is met.

For Statement

Now let us take a look at the For Loop. A for loop will run a code block several times. The FOR statement is shorter and easier to debug than other loops. This is because it contains initialization, condition, and increment or decrement on a single line.

Syntax:

for (here_we_put initialize; condition; increment/decrement)
{
// Here_we_put_the_code_block_that_we_need_to_execute
}


The loop is started using initialize. It uses a declared variable. The exit condition for the loop then gets tested in the condition section. The code block inside is run when this condition returns true.

Just think that the condition returns false or fails, and then it proceeds to the increment/decrement section. This is where the variable gets allocated an updated value. The values keep getting updated till the criteria or the condition gets met.

Frequently Asked Questions

Why do we use control statements?

The control statements allow users to choose the sequence in which the instructions in a program are executed. These enable the code to make certain decisions and do various activities repeatedly. It also even lets us jump from one piece of code to another.

What are the 5 JavaScript statements?

There are 5 categories of Javascript statements Control Flow, Declaring variables, Functions and classes, Functions and classes, and Iterations. 

What are control statement and loop?

Control statements are the ones that have the power to change the normal flow of a program's execution. Ex. If-else, switch statements. Whereas loops allow a block of code to be executed repeatedly, according to the given condition. Ex. for loops, while loops. 

What are the 3 types of functions in JavaScript?

The three types of functions in Javascript are Named functions that have specific names, anonymous functions that lack names, and arrow functions that use a concise => syntax, each serving distinct purposes in JavaScript.

Refer to know about:   jquery ajax

Conclusion

In the article, we read about Control Statements in Javascript. We read about the types of control statements that JavaScript has. We read about conditional statements and loops, which are also known as Iterative statements. Saw their syntaxes and got a basic idea of them. Refer to our courses and explore Coding Ninjas Studio to find more exciting stuff. You can also look into the interview experiences and solve different problems. Look into our Guided paths, test series, libraries and resources to know more.

Thank You

Happy Coding!

Live masterclass