Call by Value:
Suppose there is a variable named “a”. Now, we store a primitive value(boolean, integer, float, etc) in the variable “a”.
Let us store an integer value in “a”, Let a=5. Now the variable “a” stores 5 and has an address location where that primitive value sits in memory.
Now, suppose we copy the value of “a” in “b” by assignment (a=b). Now, “b” points to a new location in memory, containing the same data as variable “a”.
Thus, a=b=5 but both points to separate locations in memory.
This approach is called call by value where 2 variables become the same by copying the value but in 2 separate spots in the memory.
Features of call by value:
Function arguments are always passed by value.
It copies the value of a variable passed in a function to a local variable.
Both these variables occupy separate locations in memory. Thus, if changes are made in a particular variable it does not affect the other one.
Example:
// By value (primitives)
var a = 5;
var b;
b = a;
a = 3;
console.log(a);
console.log(b);
Output:
3
5
“b” was just a copy of “a”.
It has its own space in memory. When we change “a” it does not have any impact on the value of “b”.