Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How are variables passed to functions in JavaScript?
* JavaScript variables are always passed by value. This works as you
* expect it to when you pass primitives to functions.
function add1(x) {
// We get passed a copy of x's value -- which is a number -- so we can't
// update the original variable's value
var x = 0;
console.log("x is", x); // "x is 0"
* What happens when we pass an object? JavaScript still passes by value, but
* the value we're passing is a *reference* to the object.
function setName(obj) {
// We get passed a copy of obj's value -- which is a reference -- so we can
// still update its properties = "Nick";
var obj = {};
console.log(" is",; // " is Nick"
* Further exploration: What about boxed objects?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment