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

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -