Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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:</p> <pre><code>function alterObject(obj) { obj.foo = "goodbye"; } var myObj = { foo: "hello world" }; alterObject(myObj); alert(myObj.foo); // "goodbye" instead of "hello world" </code></pre> <p>Now, in your case, you're not passing anything anyway, as far as I can tell. You can iterate over the properties of an array with a numeric index and modify each cell of the array, if you want.</p> <p>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 <strong>calling</strong> context. So:</p> <pre><code> 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" </code></pre> <p>In a language like C++, it's possible to do that because that language <strong>does</strong> (sort-of) have pass-by-reference.</p> <p><em>edit</em> &mdash; 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 &mdash; one that <em>does</em> involve objects &mdash; one would still have the ability to modify object <em>contents</em>, and it would look pretty much exactly like it does in JavaScript. However, one would <em>also</em> be able to modify the object reference in the calling environment, and that's the key thing that you <em>can't</em> do in JavaScript. A pass-by-reference language would pass not the reference itself, but a <em>reference to the reference</em>.</p> <p><em>edit</em> &mdash; <a href="http://whatsthepointy.blogspot.com/2013/11/javascript-does-not-have-pass-by.html" rel="noreferrer">here is a blog post on the topic.</a> (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 <em>implicitly</em> when calling functions whose argument type signature calls for that to be done. Those are the key things JavaScript doesn't support.)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload