Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,709 questions

55,473 answers

573 users

How to call a function for each array element in JavaScript

2 Answers

0 votes
<button onclick="marks.forEach(myFunction)">Click</button>
var marks = [78, 92, 100, 86];

function myFunction(item, index) {
    document.write("arr[" + index + "] = " + item + "<br>");
}


/*

run:

arr[0] = 78
arr[1] = 92
arr[2] = 100
arr[3] = 86

*/

 



answered Apr 5, 2017 by avibootz
0 votes
var marks = [78, 92, 100, 86];

marks.forEach(myFunction)

function myFunction(item, index) {
    document.write("arr[" + index + "] = " + item * 2 + "<br>");
}


/*

run:

arr[0] = 156
arr[1] = 184
arr[2] = 200
arr[3] = 172

*/

 



answered Apr 5, 2017 by avibootz
...