Back

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

I would like to check whether a variable is either an array or a single value in JavaScript.

I have found a possible solution...

if (variable.constructor == Array)...

Is this the best way this can be done?

1 Answer

0 votes
by (106k points)

You can check if a variable is an array in JavaScript by using the below-mentioned code:-

if (value instanceof Array) {

alert('value is Array!'); 

else { 

alert('Not an array');

}

Related questions

+1 vote
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...