By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
Notification Show More
Latest News
Passing a JavaScript Value Between HTML Pages
Passing a JavaScript Value Between HTML Pages
JavaScript
Compare Objects in an Array
JavaScript Problem: Compare Objects in an Array
JavaScript
Switching Name Order Using Capturing Groups in Regular Expressions
Switching Name Order Using Capturing Groups in Regular Expressions
JavaScript
Shuffling an Array
JavaScript Problem: How to Perform Shuffling an Array
JavaScript
Create a Non-duplicated Collection
JavaScript Problem: Using Set to Create a Non-duplicated Collection
JavaScript
Aa
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > how to loop through two arrays in javascript
JavaScript

how to loop through two arrays in javascript

Admin
Last updated: 2022/06/23 at 2:14 PM
Admin
Share
2 Min Read
how to loop through two arrays in javascript

Hello guys, In this article we will learn how to loop through two arrays in javascript at same time. we are use forEach to loop through two arrays.

Contents
How to loop through two arrays in javascript Using forEach ?ConclusionYou may also like:

How to loop through two arrays in javascript Using forEach ?

To use forEach to loop through 2 arrays at a similar time in JavaScript, we are able to use the index parameter of the forEach callback to get the part with a similar index from the second array. If both arrays have the same length then you can use index to log elements from other array.

var array1 = [1, 2, 3, 4];
var array2 = [5, 6, 7, 8];

array1.forEach(function(item, index){
  console.log(item, array2[index])
});

Output:

1 5
2 6
3 7
4 8
how to loop through two arrays in javascript

Here we have two array array1 and array2.These two array have same length. to call array1.forEach with a callback that gets the element from array2 with the same index.

Conclusion

To use forEach to loop through 2 arrays at an similar time in JavaScript, we will use the index parameter of the forEach and get the same index from the second array.

You may also like:

  • How To Make Number Guessing Game JavaScript
  • Build a Simple Number Memorising Game In JavaScript
  • Build a Simple Digital Clock In JavaScript

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Posted by Admin
Follow:
Rocoderes is a blog you can learn HTML, CSS, JavaScript, React Js and Python along with creative coding stuff and free source code files.
Previous Article what is a proxy server What Is A Proxy Server? How Proxy Server Works? Complete Guide
Next Article How to Conduct Effective UX Research: A Guide
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

Passing a JavaScript Value Between HTML Pages

Passing a JavaScript Value Between HTML Pages

February 3, 2023
Compare Objects in an Array

JavaScript Problem: Compare Objects in an Array

January 30, 2023
Switching Name Order Using Capturing Groups in Regular Expressions

Switching Name Order Using Capturing Groups in Regular Expressions

January 29, 2023
Shuffling an Array

JavaScript Problem: How to Perform Shuffling an Array

January 27, 2023
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?