for-Each In JavaScript With Example

Introduction and Usage

for-Each In JavaScript, the forEach() function iterate the elements of an array once one by one in order. Using for-Each() function or method we also iterate object elements.

forEach In JavaScript With Example

Syntax

ArgumentDescription
currentValueThe value of the current element. (Required)
indexThe array index of the current element. (Optional)
arrThe array object the current element belongs to. (Optional)

Example of forEach in JavaScript

Output:

a
b
c

  • In the above example, we create an array.
  • Then we iterate that array element using forEach() method.
  • And method stores the output of an iterated array on that parameter.

Iterate Object Using for-Each in JavaScript

Output:

“foo: bar” “baz: 42”

  • In the above example, we create an object and assign it to a CONST type.
  • Object.entries(obj) returns an array or an object’s own enumerable string.
  • Then we use forEach() with Object.entries(obj).
  • In this process, first object converts to an array then iterate by the forEach() function.

See the Pen Foreach and Map Example JS by Bikash Panda (@phpcodertech) on CodePen.

Here is the complete explanation with example.

To know more Object.entries() – JavaScript | MDN (mozilla.org)

Also Read

Happy Coding..!

Was this article helpful?
YesNo
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.
Posts created 188

One thought on “for-Each In JavaScript With Example

Leave a Reply

Your email address will not be published.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top