# Value Type vs Reference Type
# Value Types (Primitives)
- Int
- String
- Boolean
- Symbol
- undefined
- null
# Reference Types (Objects)
- Object
- Function
- Array
# Is it a value or a reference(pointer?)
Value Types save values
let x = 10
let y = x
x = 20
console.log(y)
// 10
1
2
3
4
5
2
3
4
5
Reference Types save pointers
let x = {value:10}
let y = x
x.value = 20
console.log(y)
// {value:20}
1
2
3
4
5
2
3
4
5
scope : Value Types
let number = 10
function increase(number){
number ++;
}
increase(number)
console.log(number)
//10
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
This is because the number
value was copied into the function scope. It is independent.
scope: Reference Types
let number = {value:10}
function increase(number){
number.value ++;
}
increase(number)
console.log(number)
//{value:11}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
This is because number
reference was copied into the function. It is the same. Objects are copied by their reference!