Call by value of a primitive data is Js
String, Number, Boolean, null and undefined are primitive types of data and this type of data is passed by value.
When we create a variable and store a primitive value in it, we create in the memory a specific place for the variable and the value.
var box1 = ‘shoes’var box2 = 20
When we create another variable and assign to it the value of the variables that we created at the first place, we don’t change one variable for the other, we copy the values and assign them to the new variables.
var box1 = ‘shoes’
var box2 = 20
var newBox1 = box1
var newBox2 = box2console.log(box1, box2, newBox1, newBox1); //Returns: ‘shoes’, 20, ‘shoes’, 20
Since this variables are stored in different parts of the memory, if we change the value ofnewBox1
,it will not affect the value ofbox1
, even though assigned box1
as a value of newBox1
var box1 = ‘shoes’
var box2 = 20var newBox1 = box1
var newBox2 = box2//Reasigning valuesnewBox1 = 'dress'
newBox2 = 5console.log(box1, box2, newBox1, newBox1);
//Returns: ‘shoes’, 20, ‘dress’, 5
References