Introduction to Control Statement in JavaScript
In this article, we will understand what Control Statements are in JavaScript, how to use these statements, and the syntax for each. But before we move into the control statements part, let’s clarify our understanding of the JavaScript Programming Language.
JavaScript, widely known as JS, is a high-level, lightweight, and object-oriented programming language. JS has interpreted programming language, meaning JS code does not comply when executed, but a translator translates it. This translator translates the JS code and loads the result into a web page on a browser. A JS Engine is a simple computer program that does the translation work. It is a Standard Interpreter. JS Engine efficiently harnesses the advantage of Just-In-Time compilation for better performance.
A normal statement executes a command or performs an action in any program. Control Statements are kind of the same statements but with the power to decide which of the other statements should be executed and when. A basic example for anyone who understands programming, IF-ELSE is one of the control statements. These control statements’ power is based on an expression that does the evaluation job.
Type of Control Statement in JavaScript
Every programming language, basically, has two types of control statements as follows:
- Conditional Statements: based on an expression passed, a conditional statement makes a decision, which results in either YES or NO.
- Iterative Statements (Loop): These statements continue to repeat until the expression or condition is satisfied.
Examples
let’s understand these statements along with examples:
1. Conditional Statements
Conditional statements in a program decide the next step based on the result. They result in either True or False. The program moves to the next step if a condition is passed and true. However, if the condition is false, the program moves to another step. These statements are executed only once, unlike loop statements.
Following are the different types of Conditional Statements:
IF
When you want to check for a specific condition with the IF condition, the inner code block executes if the provided condition is satisfied.
Syntax:
if (condition) {
//code block to be executed if condition is satisfied
}
IF-ELSE
an extended version of IF. When you want to check a specific condition and two
Syntax:
if (condition)
{
// code to be executed of condition is true
}
else {
// code to be executed of condition is false
}
As observed in an IF-ELSE statement, when the condition is satisfied, the first block of code executes, and if the condition isn’t satisfied, the second block of code executes.
SWITCH
A switch statement is similar to IF and is useful when executing one code out of the multiple code block execution possibilities based on the result of the expression passed. Switch statements carry an expression, which is compared with values of the following cases, and once a match is found, the code associated with that case executes.
Syntax:
switch (expression) {
case a:
//code block to be executed
Break;
case b:
//code block to be executed
Break;
case n:
//code block to be executed
Break;
default:
//default code to be executed if none of the above case is executed
}
The above code contains an expression at the beginning, checked and compared with the cases included. If the expression passed matches with case a, the code block inside the case is executed. The same applies to cases b and n, and when the expression passed matches with none of the cases mentioned, it code enters the default case, and the code under the default case is executed.
Now that we have understood the conditional statements let’s learn about the second type, i.e. Iterative Statements.
2. Iterative Statement
Looping is a powerful tool for any programming language to execute instructions repeatedly while the expression passed is satisfied. A basic example can be printing “Hello World” 10 times. Writing the same print statement with “Hello world“ for 10 straight times will be time-consuming and impact the execution time. And this is where looping comes in handy. There are three Iterative statements: WHILE, DO-WHILE, and FOR. Let’s understand each with syntax.
WHILE
One of the control flow statements, the “while” statement, executes a code block when the condition is satisfied. The difference between “IF” and “while” is that “IF” executes code if the condition is satisfied, while “while” continues to repeat itself until the condition is satisfied, unlike “IF”, which executes code only once if the condition is satisfied, “while” keeps repeating itself until the condition is satisfied.
Syntax:
while (condition)
{
//code block to be executed when condition is satisfied
}
DO-WHILE
Similar to a while loop, with a twist that keeps a condition at the end of the loop. Also known as Exit Control Loop, DO-WHILE executes the code and checks for the condition.
Syntax:
while
{
//code block to be executed when condition is satisfied
} (condition)
If the condition at the end is satisfied, the loop will repeat.
FOR
a for loop will execute a code block a number of times. Compared to other loops, FOR is shorter and easy to debug as it contains initialization, condition, and increment or decrement in a single line.
Syntax:
for (initialize; condition; increment/decrement)
{
//code block to be executed
}
With initialization, the loop starts by using a declared variable. Then, the condition part checks the exit condition for the loop. When this condition returns true, the code block inside is executed. If the condition returns false or fails, the control moves to the increment/decrement part, where the variable is updated. The values are updated until the condition is satisfied.
Conclusion
JavaScript has two types of control statements. Conditional and Iterative (Looping) with their particular uses. We learned about conditional statements like IF, IF-ELSE, and SWITCH and their respective syntaxes. And for Iterative Statements, we learned about WHILE, DO-WHILE, and FOR, along with syntaxes. A condition is passed and checked until satisfied.
Recommended Articles
This is a guide to Control Statement in JavaScript. Here we discuss the introduction and types of control statements in javascript, including conditional statements and iterative statements. You can also go through our other suggested articles to learn more –