Condition Control Operations and Looping are the soul of every programming language,
In the large programming world, control flow is an important dimension for the creation of functional and effective applications. JavaScript is one programming language that has great applicability; numerous control structures are available to help control the flow of execution. These structures may be either conditional statements or loops, which will act as the fundamental blocks for decision-making and repetition in coding. This article looks into the structures with further details on each and various examples, making building your JavaScript skills just a walkover.
Conditional Statements in JavaScript
Conditional statements are used to make decisions for different types of actions. Conditional statements are used to control the flow of the program, applied based on the conditions. They work on the principle of truthiness or falseness of an expression. JavaScript provides the following types of conditional statements:
if statement: This type is the simplest form of checking conditions. It runs a block of code if the given condition is true.
if (temperature > 30) {
console.log("It's hot outside!");
}
else
statement: Used with if
to execute a block of code when the condition is false.
if (temperature > 30) {
console.log("It's hot outside!");
} else {
console.log("It's not hot outside!");
}
else if
statement: Used to specify a new condition if the first condition is false.
if (temperature > 30) {
console.log("It's hot outside!");
} else if (temperature < 10) {
console.log("It's cold outside!");
} else {
console.log("The weather is nice!");
}
switch statement: This specifies many alternative blocks of codes to be executed. It is like a more structured way in the possibility of multiple conditions being met. This is done by matching an expression against multiple case values.
switch (new Date().getDay()) {
case 0:
day = "Sunday";
break;
case 1:
day = "Monday";
break;
case 2:
day = "Tuesday";
break;
case 3:
day = "Wednesday";
break;
case 4:
day = "Thursday";
break;
case 5:
day = "Friday";
break;
case 6:
day = "Saturday";
break;
}
console.log(day);
Loops in JavaScript
Loops SR These are applied in order to carry out repetitive work; hence, they save time in addition to the lines of code to be written. JavaScript has various methods of looping through values: for Loops Run a block of code a specified number of times with a specified condition.
for (let i = 0; i < 5; i++) {
console.log('Number ' + i);
}
while
loop: Executes its statements as long as a specified condition evaluates to true.
let i = 0;
while (i < 5) {
console.log('Number ' + i);
i++;
}
do.while loop It’s essentially a while loop, but it will execute the block of code at least once – it will only check the condition after the code in the block has been executed, and then repeat the loop as long as the condition is true.
let i = 0;
do {
console.log('Number ' + i);
i++;
} while (i < 5);
for...in
loop: Loops through the properties of an object.
const person = {fname:"John", lname:"Doe", age:25};
for (let x in person) {
console.log(x + ": " + person[x]);
}
for...of
loop: Loops through the values of an iterable object such as an array.
const cars = ["BMW", "Volvo", "Mini"];
for (let x of cars) {
console.log(x);
}
Statements and loops, conditional or otherwise, are a very essential component of any web-based application involving interactivity and dynamism. As such, knowledge of the same is crucial for any web developer. They enable the web developer to have control over the execution or running order of a given program. The developer is able to address various programming scenarios that may arise in the course of using JavaScript.