Passing data by value
When you pass data to a function by value, this means that you are passing whatever the variable is holding, not the variable itself. Therefore, the function creates a copy and subsequently changing the copy or the original to not affect the other.
Numbers and Boolean values (true and false) are copied, passed, and compared by value. When you pass a parameter to a function by value, you are making a separate copy of that parameter, a copy that exists only inside the function.
// Global variable
num = 10;
// Change the local num
Passing data by reference
Passing by reference means that the alias representing the variable itself is received, not the copy. Since the alias (reference) represents the variable itself, changing it changes the original.
Arrays, functions and objects are typically are copied, passed, and compared by reference. When you pass a parameter by reference, and the function changes the value of that parameter, it is changed everywhere in the script.
// Define an array
days = new Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
for (key in days)
document.write("Element value is " + days[key]);