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

  • Home
  • SEARCH
  • 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 6704705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:19:09+00:00 2026-05-26T07:19:09+00:00

How do I pass variables by reference in JavaScript? I have three variables that

  • 0

How do I pass variables by reference in JavaScript?

I have three variables that I want to perform several operations to, so I want to put them in a for loop and perform the operations to each one.

Pseudocode:

myArray = new Array(var1, var2, var3);
for (var x = 0; x < myArray.length; x++){
    // Do stuff to the array
    makePretty(myArray[x]);
}
// Now do stuff to the updated variables

What is the best way to do this?

  • 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-05-26T07:19:09+00:00Added an answer on May 26, 2026 at 7:19 am

    There is no “pass by reference” available in JavaScript. You can pass an object (which is to say, you can pass-by-value a reference to an object) and then have a function modify the object contents:

    function alterObject(obj) {
      obj.foo = "goodbye";
    }
    
    var myObj = { foo: "hello world" };
    
    alterObject(myObj);
    
    alert(myObj.foo); // "goodbye" instead of "hello world"
    

    You can iterate over the properties of an array with a numeric index and modify each cell of the array, if you want.

    var arr = [1, 2, 3];
    
    for (var i = 0; i < arr.length; i++) { 
        arr[i] = arr[i] + 1; 
    }
    

    It’s important to note that “pass-by-reference” is a very specific term. It does not mean simply that it’s possible to pass a reference to a modifiable object. Instead, it means that it’s possible to pass a simple variable in such a way as to allow a function to modify that value in the calling context. So:

     function swap(a, b) {
       var tmp = a;
       a = b;
       b = tmp; //assign tmp to b
     }
    
     var x = 1, y = 2;
     swap(x, y);
    
     alert("x is " + x + ", y is " + y); // "x is 1, y is 2"
    

    In a language like C++, it’s possible to do that because that language does (sort-of) have pass-by-reference.

    edit — this recently (March 2015) blew up on Reddit again over a blog post similar to mine mentioned below, though in this case about Java. It occurred to me while reading the back-and-forth in the Reddit comments that a big part of the confusion stems from the unfortunate collision involving the word “reference”. The terminology “pass by reference” and “pass by value” predates the concept of having “objects” to work with in programming languages. It’s really not about objects at all; it’s about function parameters, and specifically how function parameters are “connected” (or not) to the calling environment. In particular, note that in a true pass-by-reference language — one that does involve objects — one would still have the ability to modify object contents, and it would look pretty much exactly like it does in JavaScript. However, one would also be able to modify the object reference in the calling environment, and that’s the key thing that you can’t do in JavaScript. A pass-by-reference language would pass not the reference itself, but a reference to the reference.

    edit — here is a blog post on the topic. (Note the comment to that post that explains that C++ doesn’t really have pass-by-reference. That is true. What C++ does have, however, is the ability to create references to plain variables, either explicitly at the point of function invocation to create a pointer, or implicitly when calling functions whose argument type signature calls for that to be done. Those are the key things JavaScript doesn’t support.)

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

Sidebar

Related Questions

I have a camera class with two reference variables. I want to pass in
Java doesn't pass variables by reference. In that case, how do data structures like
i want to pass large variables to a PHP script on another server with
I want to pass a few variables to another page. Currently I'm using response.redirect
I want to call an ASHX file and pass some query string variables from
Possible Duplicate: Is it possible to pass parameters by reference using call_user_func_array()? I have
I want to pass c# binding variable to javascript function. Here is my code:
I have the following program where two variables are to be passed by reference
I have an array witch I pass to a function by reference to sort
How can I pass variables by reference to setInterval's call back function? I don't

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.