top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to check if a variable is array in JavaScript?

+2 votes
795 views
How to check if a variable is array in JavaScript?
posted Mar 28, 2014 by Madhavi Latha

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Simplest way is

if (value instanceof Array) {
 alert('Array');
} else {
 alert('Not an array');
}

Another way is use of angular.isArray() function.
Refer: http://docs.angularjs.org/api/ng/function/angular.isArray

answer Mar 28, 2014 by Salil Agrawal
0 votes

Fastest method

variable.constructor === Array

2nd method (check the property)

variable.prop && variable.prop.constructor === Array

3rd method (easy one)

variable instanceof Array

Slowest method

Array.isArray(variable)
answer Dec 21, 2016 by Atindra Kumar Nath
Similar Questions
0 votes

I have a variable created by some rails in my controller that looks like this:

@myvar= row["myvar"]

and I want to pass it into some javascript that is referenced from within my view:

view:

js (play_time):

I am trying to pass " into the js [obviously that is not the right syntax]. Can anyone help?

+2 votes

Is there any way so that i will get to know about my variable is a number or not without using Javascript?

...