Updated March 16, 2023
Â
Â
Introduction to Conditional Statements in JavaScript
Javascript is a commonly used lightweight open-source computer programming language used to design part of web pages for the interaction of the client to the server or from the server to the client. It is basically used to manipulate, validate and calculate data. Not only this but it is also used to manipulate html and css. Commonly used javascript variables are numbers, string, object, array, and function. Under the function variable comes the conditional statement in javascript. It also has object-oriented capabilities. Earlier javascript was known as LiveScript. Javascript can also create a network-centric application.
Focusing on our topic, the conditional statement is a very important part of the syntax of programming language. It is very helpful and useful in making conditional decisions based on our program. A statement is any command or sentence that the JavaScript engine can execute to make something happen or cause some of its side effects. A conditional statement is also used to maintain data flow or program flow in javascript. A conditional statement is designed to work independently irrespective of dependencies like methods, objects, and properties.
Different Conditional Statements in JavaScript
Listed below are some of the conditional statements frequently used in javascript.
- Break
- Continue
- For
- For..in
- If…else
- New
- Return
- Var
- While
- With
So let’s discuss each of them briefly and find out the main and marginal difference between each javascript conditional statement.
1. Break
The break statement says javascript to immediately exit the controlled structure and carry on the process of execution after that point of a structure. It is used in a conditional statement in accordance with the following commands like for, for..in, and while. It is also used to completely stop the for loop or break the point of execution at that particular time.
For Example
for (Count=1; Count<=10; Count++) {
if (Count == 8)
break;
document.write ("<h1>Loop: " + Count + "</h1>");
}
2. Continue
Continue conditional statement says javascript that to immediately skip or leave the current iteration in for, fon..in, or while loop and it can process the program with the next iteration. The main use of continuing conditional statements is that they can skip the part of an instruction in a loop, but it cannot completely exit the loop like a break statement.
For Example
for (Count=1; Count<=10; Count++) {
if (Count == 7)
continue;
document.write ("<h1>Loop: " + Count + "</h1>");
}
3. For
For conditional statement, repeat a block of code in a loop for one or more times. The number of iteration in for loop is handled by the values we provide as an argument. The official syntax of for statement is :
for (InitVal; Test; Increment)Â
where,
- InitVal: The starting value of a loop is often 0 or 1, but it can be any number of choices according to requirement. It can be written as I = 1 or 0.
- Test: it is used to handle the number of iteration of the loop; as long as the loop gets the true expression, as the test expression becomes false, the loop gets over immediately. It is a condition like count< 10.
- Increment: this statement tells the javascript loop how to make increment or decrement in an iteration. It is written like count++ or i++ .
For Example
for (Count=1; Count<=10; Count++) {
document.write ("Iteration: "+Count+"<BR>");
}
4. For..in
Unlike for statement, for..in the statement does not use tests or other expressions.
for..in syntax isÂ
for (var in object) {
statements
} Â ;
Where,
- var is the name of a variable
- an object is an object you wish to examine
- statements are one or more JavaScript instructions
For Example
function test() {
for (temp in document.myform) {
alert (temp);
}
}
5. If…else
It is used to create if conditional statement with its optional else statement. It tests only a specific condition, i.e. if the expression in the program is true, the condition executes the script keeping the if statement. If the condition is false, then the conditional statement jumps to else expression in the script. Expressions in if statements are not limited to the == equality operator.
For ExampleÂ
if (ExampleVar == 10) {
Count = 1;
Start();
} else {
Count = 0;
Stop();
}
6. While
The while conditional statement creates a unique repeating loop that enables the script in a program to repeat the set of instructions. The looping carries on until the conditional statement is true. When it comes across the false expression, the loop breaks, and the late script continues.
The syntax of the while statement isÂ
while (Expression)Â {
// stuff to repeat
}
For ExampleÂ
Count=0;
Response = prompt ("Please enter a number greater than 1");
Count = 1;
while (Count <= Response) {
document.write ("Count: "+ Count + "<BR>");
Count++;
}
If statement with while conditionÂ
Response = prompt ("Please enter a number greater than 1");
Count = 1;
Response = "";
while ((right== "") || (right == "<undefined>")){
right =prompt ("Please enter your surname", "");
}
if (right != null)
alert ("Hello, " + right);
7. Return
It is used to mark the end of the function. It can be used with or without a return value. If the value is included in the statement or function, then the script return that value otherwise, it returns a null value.
For Example
function myFunc() {
var OutString = "This is a test";
return (OutString);
}
function myFunc() {
OutString = "This is a test";
return;
}
8. New
It creates a copy of an existing object. It is created in two ways :
- The date is a built-in JavaScript object, so it is used to define a new date object.
- To create a new user-defined object.
For ExampleÂ
now = new Date();
HourNow = now.getHours();
So as of now, we have covered all the important conditional statement that is used in javascript. So we can say conditional statement behaves as a glue stick to a javascript program together.
Recommended Articles
This is a guide to the Conditional Statements in JavaScript. Here we discuss the different conditional statements in javaScript, which include break, continue, For..in and If…else, etc. You may also look at the following articles to learn more –