You cannot pass by reference. If you want to modify the state of your caller from within the called function, there are two ways you could do:
Sometimes it fits best to wrap the state in an object:
var x = { valueToChange: 'initialValue' };
passByReference(x);
This works because with objects, a pointer to the address where the object lies is passed. That pointer is passed by value but it still points to the same object.
In some other cases a callback does the trick:
var x = "apple";
passByReference(function (newValue) { x = newValue; });
function passByReference(callback) {
callback("banana");
}
This works because if you define a function as above it builds a closure together with all variables it references. When your function gets called it really modifies the value of x
.