0 votes
1 view
in Web Technology by (31.7k 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 (75.1k points)

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

Array.isArray(obj)

...