0 votes
1 view
in Web Technology by (47.8k 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 (107k 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
Welcome to Intellipaat Community. Get your technical queries answered by top developers !