Javascript Module 8
Javascript Module 8
Conditional statements are used in JavaScript to make decisions based on specific conditions. There are
mainly three types of conditional statements in JavaScript: if, else if, and else. Here is an explanation of
each type:
1. if Statement:
The if statement is used to execute a block of code if a specified condition is true.
let num = 10;
if (num > 0) {
console.log('Number is positive');
}
In this example, the message 'Number is positive' will be displayed in the console if the value of num is
greater than 0.
let num = 0;
if (num > 0) {
console.log('Number is positive');
} else if (num < 0) {
console.log('Number is negative');
} else if (num%2==0) {
console.log('Number is even');
}
else if (num%2==1) {
console.log('Number is odd');
}
In this case, if num is less than zero, 'Number is negative' will be printed in the console.
3. else Statement:
The else statement is used to execute a block of code if the condition in the if statement is false.
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@bytesplash.in
Software Training and Placement Center
let num = 0;
if (num > 0) {
console.log('Number is positive');
} else {
console.log('Number is not positive')
}
Iteration statements, also known as loops, allow you to execute a block of code multiple times. There
are different types of iteration statements in JavaScript that can be used based on the specific
requirements of your program. Here are the three main types of iteration statements in JavaScript:
1. for Loop:
The for loop is commonly used when the number of iterations is known or defined.
In the for loop above, i is initialized to 0, the loop continues as long as i is less than 5, and i is
incremented by 1 in each iteration.
2. while Loop:
The while loop executes a block of code while a specified condition is true.
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
The while loop will print the value of i from 0 to 4 in the console.
3. do-while Loop :
The do-while loop is structured to execute a block of code at least once before evaluating the condition
for continuing the loop.
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@bytesplash.in
Software Training and Placement Center
let count = 0;
do {
console.log(count);
count++;
} while (count < 5);
In this example, the value of count from 0 to 4 will be logged to the console, even though the condition
is checked at the end of each iteration.
4. for...of Loop:
The for...of loop can be used to iterate over the values of an iterable object, such as arrays, strings,
maps, sets, etc.
In this example, the for...of loop iterates over each value in the numbers array and logs them to the
console.
5. for...in Loop:
The for...in loop is used to iterate over the enumerable properties of an object.
const person = {
name: 'Alice',
age: 30,
city: 'Paris'
};
In this case, the for...in loop iterates over the properties of the person object and prints out the key-
value pairs.
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@bytesplash.in
Software Training and Placement Center
6. forEach Method :
The forEach method allows you to iterate over each element in an array and execute a provided
function for each element.
numbers.forEach(function(number) {
console.log(number);
});
In the example above, the forEach method is used to loop through each element in the numbers array
and print it to the console.
- for loop: Ideal for a known number of iterations with an initialization, condition, and
increment/decrement statement.
- while loop: Executes a block of code as long as a specified condition remains true.
- do-while loop: Similar to the while loop but ensures that the block of code is executed at least once
before checking the condition for continuation.
- for...of loop: Used to iterate over the values of iterable objects like arrays.
- for...in loop: Iterates over the enumerable properties of an object.
- forEach method: Allows you to loop through each element in an array and perform an action on each
element.
In JavaScript, the continue and break keywords are used within loops to control the flow of the loop.
Here is an explanation of how these keywords work:
1. continue Keyword:
The continue keyword is used to skip the current iteration of a loop and proceed to the next iteration.
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@bytesplash.in
Software Training and Placement Center
In this example, when i is equal to 2, the continue statement skips the remaining code in that iteration
and moves to the next iteration. The output will be numbers 0, 1, 3, and 4 (skipping 2).
2. break Keyword:
The break keyword is used to exit a loop prematurely, regardless of whether the loop condition is still
true.
In this case, when i becomes equal to 3, the break statement terminates the loop immediately.
Therefore, the output will be numbers 0, 1, and 2 (stopping at 3).
Conclusion:
- continue keyword: Skips the current iteration and moves to the next iteration
- break keyword : will break and exit the whole iteration
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@bytesplash.in
Software Training and Placement Center
These iteration statements in JavaScript are powerful tools for creating dynamic and efficient code that
can handle repetitive tasks and iteration over data structures.
In JavaScript, besides the commonly used for loop and while loop, we also have for...of, for...in, and
forEach loops that provide additional functionalities for iterating over arrays and objects. Let's explore
each of these iteration methods:
Real-Time Uses:
Form Validation: Using conditions to check input validity.
User Authentication: Conditions to verify user credentials.
Iterating Over Arrays: Using loops to process elements in an array.
DOM Manipulation: Iterating through DOM elements for manipulation.
Asynchronous Operations: Loops can be used to handle asynchronous tasks.
Game Development: Conditions for game logic, loops for animations or game loops.
Data Filtering: Conditions and loops to filter data based on criteria.
Error Handling: Using conditions to handle different error scenarios.
API Calls: Loops for processing API responses or making repeated requests.
Dynamic UI Updates: Conditions and loops to dynamically update UI elements based on data changes.
Address : ByteSplash Training and Placement Center, 4th Floor, No 92/9, PR Layout, Marathahalli, Bengaluru, Karnataka, 560037
Mobile : +91 8660803099 / +91 6301062858 , Email : enquries@bytesplash.in