Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8547131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T13:12:00+00:00 2026-06-11T13:12:00+00:00

example: var arr = [one,two,three]; arr.forEach(function(part){ part = four; return four; }) alert(arr); The

  • 0

example:

var arr = ["one","two","three"];

arr.forEach(function(part){
  part = "four";
  return "four";
})

alert(arr);

The array is still with it’s original values, is there any way to have writing access to array’s elements from iterating function ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T13:12:02+00:00Added an answer on June 11, 2026 at 1:12 pm

    The callback is passed the element, the index, and the array itself.

    arr.forEach(function(part, index, theArray) {
      theArray[index] = "hello world";
    });
    

    edit — as noted in a comment, the .forEach() function can take a second argument, which will be used as the value of this in each call to the callback:

    arr.forEach(function(part, index) {
      this[index] = "hello world";
    }, arr); // use arr as this
    

    That second example shows arr itself being set up as this in the callback.One might think that the array involved in the .forEach() call might be the default value of this, but for whatever reason it’s not; this will be undefined if that second argument is not provided.

    (Note: the above stuff about this does not apply if the callback is a => function, because this is never bound to anything when such functions are invoked.)

    Also it’s important to remember that there is a whole family of similar utilities provided on the Array prototype, and many questions pop up on Stackoverflow about one function or another such that the best solution is to simply pick a different tool. You’ve got:

    • forEach for doing a thing with or to every entry in an array;
    • filter for producing a new array containing only qualifying entries;
    • map for making a one-to-one new array by transforming an existing array;
    • some to check whether at least one element in an array fits some description;
    • every to check whether all entries in an array match a description;
    • find to look for a value in an array

    and so on. MDN link

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have an array like this: var arr = ['one','two','three']; I can access
I found this example that says: var arr=$.map([0,1,2],function(n){ return [n,n+1]; }); Will make arr
I have one array in javascript like var arr = [aa,bb,cc, dd]; and now
Possible Duplicate: Javascript swap array elements For example i have an array var arr
Example: var arr_1:Array = new Array(); arr_1.push({sdate:2010-02-02,status:New}); arr_1.push({sdate:2010-02-03,status:New}); arr_1.push({sdate:2010-02-04,status:New}); arr_1.push({sdate:2010-02-05,status:New}); How can i change
I have the following array as3 example: var arrayDefinitionsargsAmfPhp:Array = new Array(); arrayDefinitionsargsAmfPhp['tabela'] =
function Convert_ONE2TWO() { var arrTWO = new Array( a, e, i); var arrONE =
Say I have the following array: var arr = new[] { A, B, C
Example for array arr = [a, b, c] # TODO create an alias for
Example: var $doesNotYetExistInTheDOM = $('<span/>'); // create new jQuery element // outside of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.