Datatypes in JavaScript

1) typeof : typeof is the function use to get the type in js.


boolean only true and false are allowed for boolean

2) Finding an object’s class

To find whether a object was constructed by by a certain constructor or one inheriting from it , we can use instanceof keyword

console.log(2 instanceof  Number)       //false
console.log('abc' instanceof String) //false
console.log(true instanceof Boolean) //false
console.log(Symbol() instanceof Symbol) //false

Every value in JS besides null and undefined also has a constructor property storing the function that was used to construct it. This even works with primitive

(that).constructor stores the function which is used to create "that" object

3) Getting object type by constructor name

with typeof operator one gets type “object” it falls into somewhat wast category in other words what type of “object” is actually it. One way to do it is to use object constructor name to get what falvour of object is actually it.

Argument pass to Object.prototype.toString.call function is value or constructor for which we are trying to find object

Bonus : Polyfill for isNumber

function isNumber(value)
{
//null.constructor and undefined.constructor throw an error
if(value === null || value === undefined) return false
return value.constructor === Number
}

I am programmer and web developer who loves to teach