Articles → JAVASCRIPT → Value Type And Reference Type In Javascript
Value Type And Reference Type In Javascript
Purpose
- In case of the value type, a memory location is allocated and the actual value is stored in it. Number, boolean, null are examples of value types.
- In case of reference type, a memory location is allocated and the reference of another memory location is stored in it. Arrays and objects are examples of the value type.
How Is The Copy Done In Value Type?
<script>
var number = 1;
var number2 = number;
console.log("number:" + number);
console.log("number2:" + number2);
number = 2;
console.log("number:" + number);
console.log("number2:" + number2);
</script>
How Is The Copy Done In Reference Type?
<script>
var person = {
name : "gyan"
};
var anotherPerson = person;
// This code copies the pointer from one object to another
person.name = "gyansangrah";
console.log(person.name);
console.log(anotherPerson.name);
</script>