If tutorials available on this website are helpful for you, please whitelist this website in your ad blocker😭 or Donate to help us ❤️ pay for the web hosting to keep the website running.
forEach() method, Array में present हर एक element के लिए एक Callback Function call करता है। Callback Function दिए गए elements के order में ही run होता है।
forEach() function Array के प्रत्येक value को user defined callback function में send करता है।
Callback Functions वो function होते हैं जो किसी दूसरे function में as an arguments pass किये जाते हैं। और जिस function में ये pass किये जाते हैं उन्हें Higher - Order Function कहते हैं।
JavaScript Callback Functions
array.forEach(function(currentValue, index, array), value);
function(currentValue, index, array) | required : यह callback function Array के हर एक element के लिए run होता है। इसमें तीन Parameters pass किये जाते हैं।
value | optional : यह value callback function में pass करने के लिए होती है , जिसे function में this variable के through access कर सकते हैं , अगर value pass नहीं करते हैं तो by default "undefined" जाता है।
Return Value : यह new undefined return करता है।
File : js_array_forEach.html
<!DOCTYPE html>
<html>
<body>
<script>
function myfun(value , index, arr)
{
/*access 10 using this*/
document.write(value*this + "<br>");
}
var arr = [2, 5, 6, 8, 8];
let new_arr = arr.forEach(myfun, 10);
</script>
</body>
</html>
20 50 60 80 80
हालाँकि अगर आप चाहे तो , अलग function define करने की वजाय forEach() function में ही direct function call कर सकते हैं।
<!DOCTYPE html> <html> <body> <script> var arr = [2, 5, 6, 8, 8]; let new_arr = arr.forEach(function myfun(value , index, arr) { /*access 10 using this*/ document.write(value*this + "<br>"); }, 10); </script> </body> </html>
ये भी same result ही produce करता है।
Hi ! My name is Rahul Kumar Rajput. I'm a back end web developer and founder of learnhindituts.com. I live in Uttar Pradesh (UP), India and I love to talk about programming as well as writing technical tutorials and tips that can help to others.
Get connected with me. :) LinkedIn Twitter Instagram Facebook