scope - mutation inside a function in javascript -
n00b question here:
suppose call function updates number or string this
var x = "well"; var helloify = function(str){ str += "hello" };
i'd expect behavior:
helloify(x); console.log(x) \\ "well hello"
but instead get
\\ "well"
doesn't "+=" change value of "x"? change persist in scope of function not in global environment?
thanks!
--confused
when call helloify(x);
pass value of x
(a string) not reference x
.
str += "hello"
modifies str
, leaves x
alone.
nb: objects addressed reference, if x
had been reference object have modified single object addressed both variables. simple strings not objects though.
Comments
Post a Comment