24
loading...
This website collects cookies to deliver better user experience
data
types can be grouped into primitive
and reference
values.primitive
and reference
values in JavaScript. In computer programming, a data type tells the compiler or interpreter what type of value a variable has.
undefined
null
string
numbers
symbol
boolean
object
primitive
and reference
values ( a value can be seen as some kind of data which will be stored in a varaible ).undefined
null
string
numbers
symbol
boolean
Object
are considered reference
or non-primitive
values. Keep in mind that arrays
and functions
are classified as Objects
stack
and heap
.stack
.reference
value is dynamic or complex. For instance, Object
can have new properties added to it.array
can have elements added or removed from it.reference
values in the heap
.value
to a variable
, the JavaScript engine will determine whether the value is a primitive
or reference
value.stack
or heap
.primitive
value to the variable, example let a = 40
. The JavaScript engine stack
var x = 10;
The JavaScript engine will create a unique identifer (Eg. A001) in the browser's memory, and the assignment operator =
will now assign or store the value 10
in that specific place we allocated for it (i.e A001).
Because the value, 10
is a primitive value, when you access **the variable
, you will use or manipulate the **actual value stored in that variable.
This means, the variable that stores a primitive value is accessed by value
When you assign any variable that contains a primitive value to a new variable, the value stored in the initial variable is created and copied to the new variable.
let a = 40;
let b = a; //assign value stored in`a` to `b`.
a
and store a value of 40
.b
and copy the value stored in a
to it.
Let's check the output of both variables
console.log(`a =${a} and b = ${b}`)
a =40 and b = 40
In the above, with let a = 40
, we allocated a location **in memory lets call that allocated location **Ax001. Meaning when we go the Ax001, we can have access to the value 40
.
When we declare b
variable, we are also creating a location in the memory so we store a value. Let's call this location Bx001
Ax001 and Bx001 will be the location for two places in-memory where we are storing values.
Now, we copied the value stored in a
to b
let a = 40;
let b = a; //copies value stored in `a` to `b`
a
into the location of b (ie. Bx001). Meaning at location Bx001 we now store the value 40
.a
, would the value stored in variable b
change too ?a = 50; //change value stored in a
console.log(`a =${a} and b=${b})
a =50 and b =40
a
does not change the value stored in b
. Eventhough , the value of a
has been assigned to b
Why ?The values
of variables a
and b
are stored in different location in memory.
The value 40
is stored in the location Ax001 of the variable a
whilst what ever value **is stored in variable b
will be kept in the location **Bx001.
Eventhough we later assign the value stored in a
to b
, they have been stored in two separate locations.
So if you change the value of variable a
, it will not change the value stored in variable b
.
Variables a *and **b * have **no relationship,(due it their different locations in-memory).
a
variable, the value of the b
variable doesn’t change.Because a
and b
are different locations in memory.heap
this is because non-primitive values have complex data structure so need some huge memory to store it. The JS engine creates a unique identifier or address **eg(NPx001), allocate some memory or space in the heap
and store the **actual value at that address
Now if the script starts executing and it comes across a non primitive
value eg.
user = {
firstName: "Emmanuel",
lastName: "Kumah"
}
user
variable is storing is a pointer, pointing to the **address **where the value is stored and not the actual value.If you looked at the in-memory value of a reference type, you’d see a memory address (or a “reference” to a spot in memory).
Because the actual value is stored in the heap and we need access to it, we will create a reference ** or some form of pointer on the variable, eg. user
and forward ** the location to the value stored in the heap to the user
variable
Now in the call stack
what we have access to, is the *reference * or pointer to the **actual value in the heap
**
Meanning, when you want to use an object or any non-primitive data type, you work on the reference of that object,rather than the actual object.
When we access a reference value, we manipulate it through reference, not through its actual value that is stored. Thus, variables that are reference values are accessed by reference
let user = {
firstName: "emmanuel",
location: "Ghana"
}
let admin = user; //assign variable to new variable
console.log(`admin is ${admin.firstName} and user is ${user.firstName}`);
admin is emmanuel and user is emmanuel
firstName
property of the user
object and see what will happen to properties in admin
object.let user = {
firstName: "emmanuel",
location: "Ghana"
}
let admin = user; // assign user to admin
console.log(`admin is ${admin.firstName} and user is ${user.firstName}`);
admin.firstName = "Stephen"; //change the firstName property
console.log(`admin is ${admin.firstName} and user is ${user.firstName}`);
admin is emmanuel and user is emmanuel
admin is Stephen and user is Stephen
user
object, it also changed the value stored in the admin
object. primitive
values or reference
values. Primitive values are data that are stored on the stack.
Primitive value is stored directly in the location that the variable acesses.
Reference values are objects that are stored in the heap
Reference value stored in the variable location is a pointer to a location in memory where the object is stored.