In javascript, there are two different data type: primitive and reference
Primitive Types | Reference Types |
---|---|
Number | Object |
String | Array |
Boolean | Function |
Null | Map |
Undefined | Set |
BigInt(ES6) | WeakMap, WeakSet(ES6) |
Symbol(ES6) |
Array, function, map, set, WeekMap, WeakSet are also objects.
Both types of variables have declaration and assignment.
<!-- Primitive Type -->
let name = "kevin";
let age = 20;
vs
<!-- Reference Type -->
let name = {name: "kevin", age: "20"};
Both types of variables are stored in memory at the declaration by using 'let'.
However, assignment works differently for the two variables.
When variables are declared, their values are stored in either a 'stack' or a 'heap'. These memory types will not be covered in this article.
As you can see, the primitive types of variables, "Kevin" and 20 are stored in the stack as values when the reference variable of the object is stored as an address.
Key differences of the two types
- Primitive variables store primitive values
- Reference variables store an address
- The primitive's value is copied
- The reference's address is copied
- Primitive values are compared
- Reference addresses are compared
- The primitive value is returned
- The reference's address is returned
Top comments (0)