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
Prev Previous commit
for-each
  • Loading branch information
thepravin committed Jan 18, 2024
commit 06f6df1a3f36dd356f5f58eb1acce1095527f448
34 changes: 34 additions & 0 deletions JsLoops/for-each.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# JavaScript forEach

## Description
The `forEach` method is used to iterate over elements in an array and execute a provided function once for each array element.

## Syntax
```javascript
array.forEach(callback(currentValue [, index [, array]]) {
// Your code here
}, thisArg);
```
### Parameters
```callback:``` Function to execute for each element. <br/>
```currentValue:``` The current element being processed in the array. <br/>
```index (optional):``` The index of the current element being processed in the array. <br/>
```array (optional):``` The array forEach was called upon. <br/>
```thisArg (optional):``` Object to use as this when executing the callback. <br/>

## Example
```javascript
const numbers = [1, 2, 3, 4, 5];

numbers.forEach(function (num, index) {
console.log(`Element at index ${index}: ${num}`);
});
```
Output :
```
Element at index 0: 1
Element at index 1: 2
Element at index 2: 3
Element at index 3: 4
Element at index 4: 5
```