DEV Community

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

Posted on

1 1

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

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

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay