DEV Community

Cover image for Know the type of Variable in JS!
Lenin Felix
Lenin Felix

Posted on

Know the type of Variable in JS!

promo

Many times (...I guess) you have wanted or tried to want to know what type is a variable, and most likely you know it since 98% of the times you are the one who declares them.

But imagine to find yourself with an API or a third party code which you have to manipulate, that already becomes more complex, since you are only there to use that little world of variables, functions, texts and others...

Although also many times it is useful to know what type are the variables in order to create conditionals that are prepared to manipulate and to maintain your code stable and that it does not crack your code of "Hello World".

For that reason I offer you a small example of functions that you can use for those occasions (I know that it is little, but it is honest work).

Basically, these are functions to which you pass your variable to indicate whether it is of a certain type or not.

It can work, for example, in some function in which you pass parameters, but you have to do different things if the parameter receives an Object or an Array.

At first sight it could not seem very useful, but at least to me, with certain code that I had to make, it helped me.


If you liked the content you can follow me on my social networks as @soyleninjs

ko-fi

promo

Top comments (2)

Collapse
 
mrchedda profile image
mr chedda

for isObject I would argue the following function is more robust

function isObject(object){
    return object.constructor === Object
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
soyleninjs profile image
Lenin Felix

Thanks, i am not see that option, and work very fine <3