trying to understand the concept of being unable to mutate strings/numbers in javascript -
in javascript, can reassign variable like:
var x = 10; x+=10; console.log(x); //prints 20 console
but if take example of seemingly similar activity unexpected result:
var originalvar = 1; changemyvar(originalvar); function changemyvar(myvar) { myvar += 1000; return myvar; } console.log(originalvar); //prints 1 console
i see 1 in same. passing variable argument function. i'm reassigning value within function. , i'm returning , printing variable. mutation?
primitives function parameters passed value in javascript. therefore myvar
in changemyvar
function not reference originalvar
new variable value of originalvar
.
Comments
Post a Comment