Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

157
Views
What to do if i want to show the first 5 elements from the array by using forEach loop?

There are 10 elements in an array. What to do if i want to show the first 5 elements from the array by using forEach loop?

let arrays =[1, 2, 4, 6, 7, 44, 5, 7, 6]
 arrays.forEach(array =>{}) //
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

To show the first 5 elements from the array by using forEach loop, you can use if statement

const arrays = [1, 2, 4, 6, 7, 44, 5, 7, 6];

arrays.forEach(function(value, index, array) {

    if(index <= 4) {
        document.write(value + "<br>");
    }

});

Where parameters:

  • Index = The index of the current element.
  • Value = The value of the current element.
  • Array = The array of the current element.

https://www.w3schools.com/jsref/jsref_foreach.asp https://www.w3schools.com/js/js_if_else.asp

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!