ES5, ES6 or ES7 take any version, but so far the winner of all sugar syntaxes to reliably check the datatypes with vanilla JavaScript is the following one-liner.
Object.prototype.toString.call(input) //boom!!
π€―
Anyone starting a new project and care about code quality, does not like adding a lot of utility third-party libraries, or anyone trying to build their library, for them below function will help in most of their conditional statements.
The above typeOf()
function will never fail and can be kept as one of the foundational utility functions on your projects.
But hang on a second, what about other primitive types like Integer
or Float
? Sure they are valid data types in other languages. Many of us may still remember your school teacher teaching them as data types. Where are they in the above code? Why just Number
datatype to define all types of numbers?
Let's cover that on a different post. Link below.