0% found this document useful (0 votes)
9 views

For Loop in Js

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

For Loop in Js

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

In JavaScript, the `for` loop is used to repeatedly execute a block of code for a specified number of

times. It consists of three parts: initialization, condition, and increment/decrement.

Here's the basic syntax of a `for` loop:

```javascript

for (initialization; condition; increment/decrement) {

// code to be executed

```

Let's break down each part:

- **Initialization**: It is used to initialize a counter variable before the loop starts. It typically sets the
initial value of the counter variable.

- **Condition**: It specifies the condition that is checked before each iteration of the loop. If the
condition evaluates to `true`, the loop continues executing. If it evaluates to `false`, the loop terminates.

- **Increment/Decrement**: It is used to update the counter variable after each iteration of the loop. It
can be used to increment or decrement the counter.

Here's an example of using a `for` loop to print numbers from 1 to 5:

```javascript

for (var i = 1; i <= 5; i++) {

console.log(i);

```
In this example, the `for` loop is initialized with `var i = 1`, which sets the initial value of the counter
variable `i` to 1. The condition `i <= 5` is checked before each iteration. If it evaluates to `true`, the loop
continues executing. After each iteration, the counter `i` is incremented by `i++`. The loop prints the
value of `i` to the console in each iteration, resulting in the output:

```

```

You can also use the `for` loop to iterate over elements in an array or perform other operations.

You might also like