Describes the use of the forEach () method in JavaScript, including javascriptforeach.

Source: Internet
Author: User
Tags javascript extension javascript array

Describes the use of the forEach () method in JavaScript, including javascriptforeach.

The forEach () method of the JavaScript array calls each element in the array.
Syntax

array.forEach(callback[, thisObject]);

The following is the detailed information about the parameters:

  • Callback: each element of the function test array.
  • ThisObject: the object used for executing the callback.

Return Value:

Returns the created array.
Compatibility:

This method is a JavaScript extension to the ECMA-262 standard; so it may not exist in other implementations of the standard. To make it work, you need to add the top of the following script code:

if (!Array.prototype.forEach){ Array.prototype.forEach = function(fun /*, thisp*/) {  var len = this.length;  if (typeof fun != "function")   throw new TypeError();  var thisp = arguments[1];  for (var i = 0; i < len; i++)  {   if (i in this)    fun.call(thisp, this[i], i, this);  } };}

Example:

This produces the following results:

[0] is 12[1] is 5[2] is 8[3] is 130[4] is 44 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.