please click here for more wordpress cource
In JavaScript, loop control statements allow you to modify the execution of loops. There are three types of loop control statements in JavaScript:
break
: Thebreak
statement terminates the current loop or switch statement and transfers control to the statement immediately following the terminated statement.
Example:
for (let i = 1; i <= 10; i++) {
if (i === 5) {
break;
}
console.log(i);
}
// Output: 1 2 3 4
continue
: Thecontinue
statement skips the current iteration of the loop and continues with the next iteration.
Example:
for (let i = 1; i <= 10; i++) {
if (i % 2 === 0) {
continue;
}
console.log(i);
}
// Output: 1 3 5 7 9
return
: Thereturn
statement can be used inside a loop to immediately stop the execution of the function and return a value.
Example:
function findIndex(arr, value) {
for (let i = 0; i < arr.length; i++) {
if (arr[i] === value) {
return i;
}
}
return -1;
}
In the above example, the return
statement is used inside a for
loop to immediately return the index of the element if it is found in the array. If the element is not found, the function returns -1
.