For Loop in JavaScript


In JavaScript, a "for" loop is a control flow statement that allows you to repeatedly execute a block of code for a specific number of iterations. It is one of the most commonly used loops and is particularly useful when the number of iterations is known or when you want to iterate over elements in an array or other data structures.

Syntax:
       for (initialization; condition; update) {
           // Code to be executed for each iteration
       }

Here's the breakdown of each part of the "for" loop:

  • Initialization: This part is executed once before the loop starts. It is typically used to initialize a loop control variable.
  • Condition: The loop continues as long as the condition is true. Before each iteration, the condition is evaluated. If the condition is false, the loop terminates, and the program continues with the next statement after the loop.
  • Update: This part is executed after each iteration of the loop. It is typically used to update the loop control variable.

Source Code

HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tutor Joes</title>
</head>
<body>
 
	<script src="js/script.js"></script>
</body>
</html>
To download raw file Click Here

script.js
/*
for (initialize variable; condition; statement) {
  // code to be executed
}
*/

for(let i=1;i<=10;i++)
{
  console.log(i);
}

let arr=[];
for(let i=0;i<100;i+=2)
{
  arr.push(i);
}
console.log(arr);
console.log(arr[49]);
To download raw file Click Here

List of Programs


JS Practical Questions & Answers


JS Practical Project