JavaScript Types
Primitive types:
number
bool
string
undefined
- is an absence of definition.null
Symbol
(ES6)
Non-primitive types:
{}
[]
function
typeof
The typeof
operator returns string of type.
The typeof null
returns 'object'
This is an actual error, which exists for years.
Arrays and Functions are Objects actually.
typeof []
returns 'object'
.
typeof function() {}
returns 'function'
.
There are standard build-in objects, like NaN
, Infinity
, Date
etc.
But there are the following types: Boolean
, Number
etc.
When we use something like this true.toString()
, JS create some kind of wrapper: Boolean(true).toString()
.
Arrays
Arrays are object.
When we have something like this: var a = [1, 2, 3]
, we actually have:
var a = {
0: 1,
1: 2,
2: 3
}
That’s why typeof []
is equal to 'object'
.
Pass by Value || Reference
Pass by value:
var a = 10;
var b = a;
a = 5;
console.log(b); // returns 10
Clone objects
let obj = { a: 'a', b: 'b' };
let clonedObj = Object.assign({}, obj);
Another way is a using of spread operator:
let obj = { a: 'a', b: 'b' };
let clonedObj = { ...obj };
Type coercion
1 == '1' => true
Object.is(-0, +0)
returns false
.