dreta
dreta

Reputation: 1036

Does passing a string to a function copy it by value or pass it by reference?

Since strings in JavaScript are basic types, does passing a string to a function create a local copy of it? I'm wondering about this since you can't modify strings after they've been created, so it would seem illogical that JavaScript VMs wouldn't just pass the string's address to the function internally.

If anybody is going to tell me that i shouldn't worry about this (this happens a lot when talking to web developers), I'm working on HTML5 games and garbage collection is a major concern, so i really need to know.

Upvotes: 14

Views: 17634

Answers (4)

Leo qiao
Leo qiao

Reputation: 23

In JavaScript, variable is binding to value. For your case, when you call addToString with an argument, the function's parameter s is binding to the value of argument, which is a string "any foo?" without passing-by-value and passing-by-reference. So, the parameter and the argument is binding to the same string. But after JavaScript interpret the s += ", foo yes" , the parameter s is binding to a new string, which is "any foo?, foo yes". The argument s is still binding to string "any foo?" whenever how many times you call this function with s.

Upvotes: 0

john k
john k

Reputation: 6614

The currently accepted answer is wrong. Test it yourself. Write to a string in a function asyncronously. If it were passed by reference it would update the same string. It does not.

async function addToString(s){

    console.log('adding foo to empty string')
    s += ", foo yes"
  return s

}

let s = "any foo? "
let promises = []
promises.push(addToString(s))
promises.push(addToString(s))
promises.push(addToString(s))
Promise.all(promises).then(results => console.log("s results:", s))

results:

"s results:", "any foo? "

strings are a primitive data type in javascript

and are therefore passed by value. "Any changes made to this value in the function do not affect the original variable."

Upvotes: 0

laktak
laktak

Reputation: 60043

The string will be passed by reference.

A string is not mutable so whenever you try to change it you get a new string (eg. by doing value+="more").

Also see: What does immutable mean?

@T.J. Crowder: by value vs by ref - if you are looking at the language definition you are correct. However I don't think there is an implementation that actually creates a copy of the string because it would be incredibly slow. Also since strings are immutable primitives there is no need to copy them since they can't change.

Upvotes: 15

T.J. Crowder
T.J. Crowder

Reputation: 1075099

I believe the specification is silent on this point. However, it would be a truly idiotic implementation that passed the actual content of the string rather than passing a reference to that content in memory, even if strings are theoretically "primitives". I suspect most implementations treat "primitive" strings much as they treat object references (in this regard, obviously not in some others, such as ===), but just not with the Object trappings.

Upvotes: 6

Related Questions