There are 4 ways to defind data types of JavaScript: 1. typeof, 2. instance of, 3. Object.prototype.toString.call(), 4. constructor. hope this helps.

JavaScript data types

JavaScript has 8 built-in types, expecting object, they are all called “basic type”. Which are:

null
undefined
boolean
number
string
object
symbol
bigInt

1. typeof

typeof is not a function but a operator, it returns a string indicating the type of the unevaluated operand, includes number, boolean, symbol, string, object, undefined, function and bigInt.

javascript
1
2
3
4
5
6
7
8
9
10
11
12
13
14
console.log(typeof undefined) // undefind
console.log(typeof null) // object
console.log(typeof true) // boolean
console.log(typeof 43) // number
console.log(typeof '21') // string
console.log(typeof {a:1}) // object
console.log(typeof Symbol()) // symbol
console.log(typeof 123n) // bigint
function a() {}
console.log(typeof a) // function
var date = new Date()
var error = new Error()
console.log(typeof date) // object
console.log(typeof error) // object

2. instanceof

instanceof operator is used to see if prototype property A a constructor is appears anywhere in the prototype chain of B, the return value is a boolean value.

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
console.log(12 instanceof Number)  // false
console.log('22' instanceof String) // false
console.log(true instanceof Boolean) // false
console.log(null instanceof Object) // false
console.log(undefined instanceof Object) // false

console.log([] instanceof Array) // true
console.log({a: 1} instanceof Object) // true
console.log(json instanceof Object) // true
function a() {}
console.log(a instanceof Function) // true
console.log(new Date() instanceof Date) //true
console.log(reg instanceof RegExp) //true
console.log(error instanceof Error) // true

3. Object.prototype.toString.call()

toString() is an origin method of Object, to use, it simply return its [[Class]]. This is an internal attribute, its syntax is [object Xxx], Xxx is the type of object.
For anyother objects, inorder to get the right type, needs to use call/apply method.

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
console.log(Object.prototype.toString.call(1))          // [object Number]
console.log(Object.prototype.toString.call(1n)) // [object BigInt]
console.log(Object.prototype.toString.call('123')) // [object String]
console.log(Object.prototype.toString.call(true)) // [object Boolean]
console.log(Object.prototype.toString.call(undefined)) // [object Undefined]
console.log(Object.prototype.toString.call(null)) // [object Null]
console.log(Object.prototype.toString.call({})) // [object Object]
console.log(Object.prototype.toString.call([])) // [object Array]
console.log(Object.prototype.toString.call(function a() {})) // [object Function]
console.log(Object.prototype.toString.call(Symbol())) // [object Symbol]
console.log(Object.prototype.toString.call(Math)) // [object Math]
console.log(Object.prototype.toString.call(JSON)) // [object JSON]
console.log(Object.prototype.toString.call(new Date())) // [object Date]
console.log(Object.prototype.toString.call(new RegExp())) // [object RegExp]
console.log(Object.prototype.toString.call(new Error)) // [object Error]
console.log(Object.prototype.toString.call(window) // [object Window]
console.log(Object.prototype.toString.call(document) // [object HTMLDocument]

4.constructor

The examples from the previous chapters are limited. They only create single objects.
Sometimes we need a “blueprint” for creating many objects of the same “type”.
The way to create an “object type”, is to use an object constructor function.

Code
1
2
3
4
5
6
7
8
9
10
console.log('22'.constructor === String)             // true
console.log(true.constructor === Boolean) // true
console.log([].constructor === Array) // true
console.log(document.constructor === HTMLDocument) // true
console.log(window.constructor === Window) // true
console.log(new Number(22).constructor === Number) // true
console.log(new Function().constructor === Function) // true
console.log((new Date()).constructor === Date) // true
console.log(new RegExp().constructor === RegExp) // true
console.log(new Error().constructor === Error) // true

Note:
null and undefined is invaild object, so it does not have a constructor property.