Skip to content

Instantly share code, notes, and snippets.

@carlwiedemann
Created May 12, 2018 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlwiedemann/6a2cd2730fd031333e330fd1fb2cd88e to your computer and use it in GitHub Desktop.
Save carlwiedemann/6a2cd2730fd031333e330fd1fb2cd88e to your computer and use it in GitHub Desktop.
// See https://stackoverflow.com/questions/13104494/does-javascript-pass-by-reference
(function(){
var foo = [
'hello'
];
var replace_the_argument = function (some_array) {
some_array = ['hola'];
return some_array;
};
console.log('result of replace_the_argument():');
console.log(replace_the_argument(foo));
console.log('original:');
console.log(foo);
})();
(function(){
var foo = [
'hello'
];
var mutate_the_argument = function (some_array) {
some_array.push('goodbye');
return some_array;
};
console.log('result of mutate_the_argument():');
console.log(mutate_the_argument(foo));
console.log('original:');
console.log(foo);
})();
@carlwiedemann
Copy link
Author

Result:

$ node reference.js
result of replace_the_argument():
[ 'hola' ]
original:
[ 'hello' ]
result of mutate_the_argument():
[ 'hello', 'goodbye' ]
original:
[ 'hello', 'goodbye' ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment