Store by value:
Primitive data types such as numbers, strings, booleans, null, undefined, and symbols are stored by value. When a variable of a primitive type is assigned to another variable or passed as an argument to a function, a copy of the value is created and stored in the new variable. Any changes made to one variable do not affect the other variable because they hold independent copies of the value.
Example:
let a = 5;
let b = a;
b = 10;
console.log(a); // Output: 5
console.log(b); // Output: 10
Store by reference:
Objects, arrays, and functions are stored by reference.When an object is assigned to a variable or passed as an argument, the variable holds a reference or a pointer to the memory location where the object is stored. So, if we assign the same object to multiple variables or pass it to functions, all those variables or functions will refer to the same object in memory. Therefore, modifying the object through one variable will affect all the references to that object.
Example:
let arr1 = [1, 2, 3];
let arr2 = arr1;
arr2.push(4);
console.log(arr1); // Output: [1, 2, 3, 4]
console.log(arr2); // Output: [1, 2, 3, 4]
In summary, storing by value creates independent copies, while storing by reference creates references to the same data in memory, leading to shared changes.