JavaScript foreach Function Examples

JavaScript foreach

This article is written just to introduce you with Foreach loop and how you can efficiently use in different places. I hope you will able to understand it well and do let me know in the comment section if something is incorrect or can be added to make this post valuable.

ForEach() function is used to iterate through the items which can be array.

Array. (function(currentValue, index, arr), thisValue)

Here function is the callback function and  currentValue is the element which is being processed, index is optional which meant to be the array index of the current element and arr is also option is the array object to which current element belongs to.

JavaScript array foreach

const numbers = [‘1234’, ‘2345’, ‘4563’, ‘4567’];

numbers.forEach((numberindexarr) => {

  console.log(number,index, arr.length);

if(index+1==arr.length)

{

  console.log(“Length of array is “+arr.length);

}

});

Output will be

1234 0 4

2345 1 4

4563 2 4

4567 3 4

Length of array is 4

JavaScript foreach example

function func() {

    const items = [1, 2, 3];

    const mult = [];

      items.forEach(function(item){

        mult.push(item*item);

    });

      document.write(mult);

}

func();

Output will be 1, 4, 9

In the above example each element in the array is multiplied to itself in block (item*item), 1×1, 2×2, 3×3 and then pushed to another array which is mult.

JavaScript foreach array of strings

const arraystring = [‘A’, ‘B’, ‘C’, ‘D’];

arraystring.forEach((letterindexarr) => {

  console.log(letter,index, arr);

});

Output will be

A0

(4) [ “A” ,”B” ,”C” ,”D” ]

B1

(4)[ “A” ,”B” ,”C” ,”D” ]

C2

(4)[ “A” ,”B” ,”C” ,”D” ]

D3

(4)[ “A” ,”B” ,”C” ,”D” ]

JavaScript foreach break

var Break = {};

const numbers = [‘1’, ‘2’, ‘3’, ‘4’];

try {

  numbers.forEach(function(el) {

    console.log(el);

    if (el == 2) throw Break;

  });

} catch (e) {

  if (e !== Break) throw e;

}

Output will be

1

2

JavaScript foreach break Example

var Break = {};

const arraystring = [‘Dog’, ‘Cat’, ‘Cow’, ‘Horse’];

try {

  arraystring.forEach(function(el) {

    console.log(el);

    if (el == ‘Cow’) throw Break;

  });

} catch (e) {

  if (e !== Break) throw e;

}

Output will be

Dog

Cat

Cow

JavaScript foreach continue

const arraycar = [‘BMW’, ‘Nissan’, ‘Ram’, ‘Audi’];

arraycar.forEach(function (carindex) {

  // If the car is Ram, skip it

  if (car === ‘Ram’) return;

  // Otherwise log it to the console

  console.log(car);

});

Output will be

BMW

Nissan

Audi

JavaScript foreach index

const checkindex = [‘A@S’, ‘XCD’, ‘ERF’, ‘DFG’];

checkindex.forEach(function (checkindex) {

  // If the word is ERF, check index

  if (check === ‘ERF’)

  {

console.log(check +” has index “+index);

  }

 });

Output will be ERF has index 2

JavaScript foreach index undefined

JavaScript foreach return

JavaScript foreach async

JavaScript foreach countdown

function countdown(number){

  if (number < 1) {

    return [];

  } else {

    const array = countdown(number – 1);

    array.unshift(number);

    return array;

  }

}

console.log(countdown(6));

Output

(6)[,6 ,5 ,4 ,3 ,2 ,1] will be

JavaScript foreach order

const letters = [‘C’,’A’,’B’,’D’, ‘2’, ‘1’];

letters.sort();

console.log(letters)

(6)[ “1” ,”2″ ,”A” ,”B” ,”C” ,”D” ]

 

For More JavaScript

Sum of Array

Leave a Comment

error: Content is protected !!