Back
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?
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');}
if (value instanceof Array) {
alert('value is Array!');
}
else {
alert('Not an array');
31k questions
32.8k answers
501 comments
693 users