Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I'm trying to write a function that either accepts a list of strings or a single string. If it's a string, then I want to convert it to an array with just one item. Then I can loop over it without fear of an error.

So how do I check if the variable is an array?

I've rounded up the various solutions below and created a jsperf test.

1 Answer

0 votes
by (106k points)
edited by

To check if an object is an array you can use the following:-

Array.isArray(obj)

Check out Web Development Course and enroll today! 

Related questions

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...