Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: Add JS Loop #182

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
for-Loop
  • Loading branch information
thepravin committed Jan 18, 2024
commit a60e7156f3249f429eac1a0ab0d9862382a67032
59 changes: 59 additions & 0 deletions JsLoops/for-loop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# JavaScript For Loop

## Description

The JavaScript `for` loop is a control flow statement that allows you to execute a block of code repeatedly. It consists of three optional expressions: initialization, condition, and iteration. The loop continues to execute as long as the condition is true. Here's the basic syntax:

```javascript
for (initialization; condition; iteration) {
// code to be executed
}
```
## Features
Initialization: Declare and initialize a loop variable. <br/>
Condition: Specify the condition to continue the loop. <br/>
Iteration: Define how the loop variable changes after each iteration. <br/>
## Example
### Simple Numeric Loop
```javascript
for (let i = 0; i < 5; i++) {
console.log(i);
}

```
This will output:
```
0
1
2
3
4
```
### Looping Through an Array
```javascript
const fruits = ['apple', 'orange', 'banana'];

for (let i = 0; i < fruits.length; i++) {
console.log(fruits[i]);
}
```
This will output:
```
apple
orange
banana
```
### Backward Loop
```javascript
for (let i = 5; i > 0; i--) {
console.log(i);
}
```
This will output:
```
5
4
3
2
1
```