I am a javascript beginner and I would like to create a function named swap that swaps the variables that were passed in:
function swap(a, b){
var t = a;
a = b;
b = t;
}
var x = 3;
var y = 4;
swap (x, y);
console.log(x + " " + y);
and the output should be 4 3
but I can't figure out how to make this function work, because in C++ we would just pass the reference swap(&a, &b)
. How to pass reference in JS?