1

I'm doing the following sort of code in a project of mine, and the result of it is confusing me.

function changeSomethingsValue (thingToChange) {
 thingToChange = "something else";
}
var something = "something";
changeSomethingsValue(something);
console.log(something); // prints out "something"

I'm confused as to why the value of the something variable is still the string "something" after it had been passed through the changeSomethingsValue function. Why is this happening and how do I get the function to assign the new value to whatever variable gets passed in?

dumbass
27.2k4 gold badges43 silver badges77 bronze badges
asked Sep 2, 2014 at 22:09
6
  • 1
    You don't, that's not how JavaScript works. You can change an object's properties from its reference, though. Commented Sep 2, 2014 at 22:14
  • 1
    Why the down-vote, though? I'd think this is a valid question. Commented Sep 2, 2014 at 22:18
  • 1
    arguments are private variables, so you're only changing a copy of the argument, not the upstream original string. it wouldn't matter if you passed an object, if you replace the local, nothing happens outside the function. you can re-assign something in the function (instead of thingToChange ), and that would affect the original (but not the local). Commented Sep 2, 2014 at 22:19
  • @jfrej Probably because it's a mega-dupe, and tends to be searchable, but I'm guessing. Commented Sep 2, 2014 at 22:53
  • 1
    JavaScript is pass-by-value. Assigning a value to a variable or property does not change the value of another variable or property (exceptions: global scope, with, fancy things like getters). Commented Sep 2, 2014 at 22:54

2 Answers 2

4

Parameters are passed by value, not by reference. In the case of a string, the parameter is a copy of the reference to the string. Changing the copy inside the function by assigning a new reference to it won't change the original.

If you pass in an object or array, you can change the items in them without the need to change the reference to the object or array:

function changeSomethingsValue(thingToChange) {
 thingToChange[0] = "something else";
}
var something = [ "something" ];
changeSomethingsValue(something);
console.log(something[0]); // prints out "something else"

A common pattern when a function should have one value as input and a changed value as output is to return the value:

function changeSomethingsValue(thingToChange) {
 return thingToChange + " else";
}
var something = "something";
something = changeSomethingsValue(something);
console.log(something); // prints out "something else"
answered Sep 2, 2014 at 22:18
Sign up to request clarification or add additional context in comments.

Comments

3

When passing primitives, like a string or a number, it's passed by value. What you're expecting is for it to be passed by reference.

The fix would be to set something to the return value of changeSomethingsValue

function changeSomethingsValue (thingToChange) {
 thingToChange = "something else";
 return thingToChange;
}
var something = "something";
something = changeSomethingsValue(something);
console.log(something); // prints out "something"
answered Sep 2, 2014 at 22:12

5 Comments

If you're going this route then you should use the return keyword in the function.
And if you're going this route, it doesn't make sense to use thingToChange parameter and pass something into the function.
If you passed an object and reassigned the parameter reference in the function it wouldn't do what the OP is asking either.
@jfrej, right obviously -- this whole function doesn't make sense, but it was in response to the question. @DaveNewton, you're right, it's not changing the reference, but it's ultimately changing the value of something which I do believe is what he was looking for -- not a deep understanding of passing references around in javascript.
FYI, objects are also pass by value. Passing an object instead would exhibit the same behavior.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.