How can I check if an object is an array?

JavascriptArraysJavascript Objects

Javascript Problem Overview


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 the one item so I can loop over it without fear of an error.

So how do I check if the variable is an array?

Javascript Solutions


Solution 1 - Javascript

The method given in the ECMAScript standard to find the class of Object is to use the toString method from Object.prototype.

if(Object.prototype.toString.call(someVar) === '[object Array]') {
    alert('Array!');
}

Or you could use typeof to test if it is a string:

if(typeof someVar === 'string') {
    someVar = [someVar];
}

Or if you're not concerned about performance, you could just do a concat to a new empty Array.

someVar = [].concat(someVar);

There's also the constructor which you can query directly:

if (somevar.constructor.name == "Array") {
    // do something
}

Check out a thorough treatment from T.J. Crowder's blog, as posted in his comment below.

Check out this benchmark to get an idea which method performs better: http://jsben.ch/#/QgYAV

From @Bharath, convert a string to an array using ES6 for the question asked:

const convertStringToArray = (object) => {
   return (typeof object === 'string') ? Array(object) : object
}

Suppose:

let m = 'bla'
let n = ['bla','Meow']
let y = convertStringToArray(m)
let z = convertStringToArray(n)
console.log('check y: '+JSON.stringify(y)) . // check y: ['bla']
console.log('check y: '+JSON.stringify(z)) . // check y: ['bla','Meow']

Solution 2 - Javascript

In modern browsers you can do:

Array.isArray(obj)

(Supported by Chrome 5, Firefox 4.0, Internet Explorer 9, Opera 10.5 and Safari 5)

For backward compatibility you can add the following:

// Only implement if no native implementation is available
if (typeof Array.isArray === 'undefined') {
  Array.isArray = function(obj) {
    return Object.prototype.toString.call(obj) === '[object Array]';
  }
};

If you use jQuery you can use jQuery.isArray(obj) or $.isArray(obj). If you use Underscore.js you can use _.isArray(obj).

If you don't need to detect arrays created in different frames you can also just use instanceof:

obj instanceof Array

Solution 3 - Javascript

I would first check if your implementation supports isArray:

if (Array.isArray)
    return Array.isArray(v);

You could also try using the instanceof operator

v instanceof Array

Solution 4 - Javascript

jQuery also offers an $.isArray() method:

var a = ["A", "AA", "AAA"];

if($.isArray(a)) {
  alert("a is an array!");
} else {
  alert("a is not an array!");
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Solution 5 - Javascript

This is the fastest among all methods (all browsers supported):

function isArray(obj){
    return !!obj && obj.constructor === Array;
}

Solution 6 - Javascript

Imagine you have this array below:

var arr = [1,2,3,4,5];

JavaScript (new and older browsers):

function isArray(arr) {
  return arr.constructor.toString().indexOf("Array") > -1;
}

or

function isArray(arr) {
  return arr instanceof Array;
}

or

function isArray(arr) {
  return Object.prototype.toString.call(arr) === '[object Array]';
}

Then call it like this:

isArray(arr);

JavaScript (Internet Explorer 9+, Chrome 5+, Firefox 4+, Safari 5+, and Opera 10.5+)

Array.isArray(arr);

jQuery:

$.isArray(arr);

Angular:

angular.isArray(arr);

Underscore.js and Lodash:

_.isArray(arr);

Solution 7 - Javascript

Array.isArray works fast, but it isn't supported by all versions of browsers.

So you could make an exception for others and use a universal method:

    Utils = {};
    Utils.isArray = ('isArray' in Array) ?
        Array.isArray :
        function (value) {
            return Object.prototype.toString.call(value) === '[object Array]';
        }

Solution 8 - Javascript

A simple function to check this:

function isArray(object)
{
    return object.constructor === Array;
}

Solution 9 - Javascript

As MDN says in here:

> use Array.isArray or Object.prototype.toString.call to differentiate > regular objects from arrays

Like this:

  • Object.prototype.toString.call(arr) === '[object Array]', or

  • Array.isArray(arr)

Solution 10 - Javascript

There's just one line solution for this question

x instanceof Array

where x is the variable it will return true if x is an array and false if it is not.

Solution 11 - Javascript

You can use Array.isArray(). Here is a polyfill:

if (Array.isArray == null) {
  Array.isArray = (arr) => Object.prototype.toString.call(arr) === "[object Array]"
}

Solution 12 - Javascript

I would make a function to test the type of object you are dealing with...

function whatAmI(me){ return Object.prototype.toString.call(me).split(/\W/)[2]; }

// tests
console.log(
  whatAmI(["aiming","@"]),
  whatAmI({living:4,breathing:4}),
  whatAmI(function(ing){ return ing+" to the global window" }),
  whatAmI("going to do with you?")
);

// output: Array Object Function String

then you can write a simple if statement...

if(whatAmI(myVar) === "Array"){
    // do array stuff
} else { // could also check `if(whatAmI(myVar) === "String")` here to be sure
    // do string stuff
}

Solution 13 - Javascript

You can check the type of your variable whether it is an array with;

var myArray=[];

if(myArray instanceof Array)
{
....
}

Solution 14 - Javascript

I do this in a very simple way. It works for me.

Array.prototype.isArray = true;

a=[]; b={};
a.isArray  // true
b.isArray  // (undefined -> false)

Solution 15 - Javascript

This is my attempt to improve on this answer taking into account the comments:

var isArray = myArray && myArray.constructor === Array;

It gets rid of the if/else, and accounts for the possibility of the array being null or undefined

Solution 16 - Javascript

I know, that people are looking for some kind of raw JavaScript approach. But if you want think less about it, take a look at Underscore.js' isArray:

_.isArray(object)

It returns true if object is an Array.

(function(){ return _.isArray(arguments); })();
=> false
_.isArray([1,2,3]);
=> true

Solution 17 - Javascript

I have updated the [jsperf fiddle][1] with two alternative methods as well as error checking.

It turns out that the method defining a constant value in the 'Object' and 'Array' prototypes is faster than any of the other methods. It is a somewhat surprising result.

/* Initialisation */
Object.prototype.isArray = function() {
  return false;
};
Array.prototype.isArray = function() {
  return true;
};
Object.prototype._isArray = false;
Array.prototype._isArray = true;

var arr = ["1", "2"];
var noarr = "1";

/* Method 1 (function) */
if (arr.isArray()) document.write("arr is an array according to function<br/>");
if (!noarr.isArray()) document.write("noarr is not an array according to function<br/>");
/* Method 2 (value) - **** FASTEST ***** */
if (arr._isArray) document.write("arr is an array according to member value<br/>");
if (!noarr._isArray) document.write("noarr is not an array according to member value<br/>");

These two methods do not work if the variable takes the undefined value, but they do work if you are certain that they have a value. With regards to checking with performance in mind if a value is an array or a single value, the second method looks like a valid fast method. It is slightly faster than 'instanceof' on Chrome, twice as fast as the second best method in Internet Explorer, Opera and Safari (on my machine).

[1]: http://jsperf.com/check-isarray/3 "jsperf"

Solution 18 - Javascript

The best practice is to compare it using constructor, something like this

if(some_variable.constructor === Array){
  // do something
}

You can use other methods too, like typeOf, converting it to a string and then comparing, but comparing it with dataType is always a better approach.

Solution 19 - Javascript

The best solution I've seen is a cross-browser replacement for typeof. Check Angus Croll's solution.

The TL;DR version is below, but the article is a great discussion of the issue so you should read it if you have time.

Object.toType = function(obj) {
    return ({}).toString.call(obj).match(/\s([a-z|A-Z]+)/)[1].toLowerCase();
}
// ... and usage:
Object.toType([1,2,3]); //"array" (all browsers)

// or to test...
var shouldBeAnArray = [1,2,3];
if(Object.toType(shouldBeAnArray) === 'array'){/* do stuff */};

Solution 20 - Javascript

If the only two kinds of values that could be passed to this function are a string or an array of strings, keep it simple and use a typeof check for the string possibility:

function someFunc(arg) {
    var arr = (typeof arg == "string") ? [arg] : arg;
}

Solution 21 - Javascript

Here's my lazy approach:

if (Array.prototype.array_ === undefined) {
  Array.prototype.array_ = true;
}

// ...

var test = [],
    wat = {};

console.log(test.array_ === true); // true
console.log(wat.array_ === true);  // false

I know it's sacrilege to "mess with" the prototype, but it appears to perform significantly better than the recommended toString method.

Note: A pitfall of this approach is that it wont work across iframe boundaries, but for my use case this is not an issue.

Solution 22 - Javascript

There is a nice example in Stoyan Stefanov's book JavaScript Patterns which is supposed to handle all possible problems as well as use the ECMAScript 5 method Array.isArray().

So here it is:

if (typeof Array.isArray === "undefined") {
    Array.isArray = function (arg) {
        return Object.prototype.toString.call(arg) === "[object Array]";
    };
}

By the way, if you are using jQuery, you can use its method $.isArray().

Solution 23 - Javascript

The following could be used if you know that your object doesn't have a concat method.

var arr = []; if (typeof arr.concat === 'function') { console.log("It's an array"); }

Solution 24 - Javascript

This function will turn almost anything into an array:

function arr(x) {
    if(x === null || x === undefined) {
        return [];
    }
    if(Array.isArray(x)) {
        return x;
    }
    if(isString(x) || isNumber(x)) {
        return [x];
    }
    if(x[Symbol.iterator] !== undefined || x.length !== undefined) {
        return Array.from(x);
    }
    return [x];
}

function isString(x) {
    return Object.prototype.toString.call(x) === "[object String]"
}

function isNumber(x) {
    return Object.prototype.toString.call(x) === "[object Number]"
}

It uses some newer browser features so you may want to polyfill this for maximum support.

Examples:

> arr(null);
[]
> arr(undefined)
[]
> arr(3.14)
[ 3.14 ]
> arr(1/0)
[ Infinity ]
> gen = function*() { yield 1; yield 2; yield 3; }
[Function: gen]
> arr(gen())
[ 1, 2, 3 ]
> arr([4,5,6])
[ 4, 5, 6 ]
> arr("foo")
[ 'foo' ]

N.B. strings will be converted into an array with a single element instead of an array of chars. Delete the isString check if you would prefer it the other way around.

I've used Array.isArray here because it's the most robust and also simplest.

Solution 25 - Javascript

You could use the isArray method, but I would prefer to check with:

Object.getPrototypeOf(yourvariable) === Array.prototype

Solution 26 - Javascript

var a = [], b = {};

console.log(a.constructor.name == "Array");
console.log(b.constructor.name == "Object");

Solution 27 - Javascript

A = [1,2,3]
console.log(A.map == [].map)

In search for the shortest version, here is what I got so far.

Note, there is no perfect function that will always detect all possible combinations. It is better to know all abilities and limitations of your tools than expect a magic tool.

Solution 28 - Javascript

function isArray(value) {
    if (value) {
        if (typeof value === 'object') {
            return (Object.prototype.toString.call(value) == '[object Array]')
        }
    }
    return false;
}

var ar = ["ff","tt"]
alert(isArray(ar))

Solution 29 - Javascript

A simple function for testing if an input value is an array is the following:

function isArray(value)
{
  return Object.prototype.toString.call(value) === '[object Array]';
}

This works cross browser, and with older browsers. This is pulled from T.J. Crowders' blog post

Solution 30 - Javascript

You can try this:

var arr = []; (or) arr = new Array();
var obj = {}; (or) arr = new Object();

arr.constructor.prototype.hasOwnProperty('push') //true

obj.constructor.prototype.hasOwnProperty('push') // false

Solution 31 - Javascript

The easiest and fastest way to check if an Object is an Array or not.

var arr = [];
arr.constructor.name === 'Array'  // Returns true;

or

arr.constructor === Array // Returns true;

Or you can make a utility function:

const isArray = (obj) => !!obj && obj.constructor === Array;

Usage:

isArray(arr); // Returns true

Solution 32 - Javascript

In your case you may use concat method of Array which can accept single objects as well as array (and even combined):

function myFunc(stringOrArray)
{
  var arr = [].concat(stringOrArray);
	
  console.log(arr);
  
  arr.forEach(function(item, i)
  {
    console.log(i, "=", item);
  })
}

myFunc("one string");

myFunc(["one string", "second", "third"]);

concat seems to be one of the oldest methods of Array (even IE 5.5 knows it well).

Solution 33 - Javascript

Exotic one

You want to check if the parameter is a string or not - so try

x===x+''

let isStr = x=> x===x+'';

console.log( isStr([]) );
console.log( isStr(["aa","bb"]) );
console.log( isStr("") );
console.log( isStr("abc") );

Solution 34 - Javascript

Thankfully, ECMAScript 5 introduced Array.isArray() back in December 2009. If for some reason, you are using a version of JavaScript older than ECMAScript 5, please upgrade.

If you insist on it, though, then arrays do have certain properties that differentiate them from any other type. Properties that I haven't seen mentioned in any of the other answers. Let's get into some JavaScript politics.

An array is an object (typeof [] === "object"), but unlike traditional objects, they have a length property (typeof ( {} ).length === "undefined"). null is also an object (typeof null === "object"), but you can't access a property of null because null is not an object.

This is a bug in the specification that goes all the way back to the very beginning of JavaScript, when objects had the type tag 0 and null was represented as a literal null pointer 0x00, which caused the interpreter to confuse it with objects.

Unfortunately, this doesn't account for [] vs. {length:0}. So we must now turn to the prototype chain.

( [] ).__proto__ === Array.prototype && ( [] ).__proto__ !== Object.prototype.

Thus, without Array.isArray(), this is just about the closest we can get:

function is_array(array){
    return array !== null
        && typeof array === "object"
        && array.__proto__ === Array.prototype;
}

[ [], [1,2,3], {length: 0}, {},
  1, 0, Infinity, NaN, "1", "[1,2,3]",
  null, undefined, [null], [undefined], {a:[]},
  [{}], [{length: 0}], [Infinity], [NaN],
  {__proto__: Array.prototype}
].filter(is_array)
// Expected: [ [], [1,2,3], [null], [undefined], [{}], [{length: 0}], [Infinity], [NaN] ]
// Actual:   [ [], [1,2,3], [null], [undefined], [{}], [{length: 0}], [Infinity], [NaN], {__proto__: Array.prototype} ]

The object maliciously designed to look just like an array actually passes the Turing test. However, replacing the prototype chain with the Array prototype chain is enough to make it act just like an array, effectively making it an array.

The only thing in the world that can tell such an object is actually not an array, is Array.isArray(). But for the purposes you would usually be checking if an object is an array, said object should play nice with your code.

Even the behavior when you change the length of the array artificially is the same: if the length is longer than the number of elements in the array, you will have "empty slots" of that special "implicit undefined" type that is somehow distinct from undefined while also being === undefined; the very same type that is the reason we use typeof obj !== "undefined" to avoid throwing a ReferenceError because obj === undefined only doesn't throw an error if obj was explicitly defined as undefined.

a = {__proto__: Array.prototype}; // Array {}
a.push(5)
a // [5]
a.length = 5
a // [5, empty x 4]
b = a.map(n => n*n) // [25, empty x 4]
b.push(undefined)
b.push(undefined)
b // [25, empty x 4, undefined, undefined]
b[1] // undefined
b[1] === b[5] // true
Array.isArray(a) // false
Array.isArray(b) // true

Don't use is_array(), though. It's one thing to reinvent the wheel for learning purposes. It's another thing to do it in production code. Don't even use it as a polyfill. Supporting old JavaScript versions means supporting old browsers means encouraging the use of insecure software means putting the user at risk for malware.

Solution 35 - Javascript

Although there's some solid answers I would prefer a functional approach using a functor. A functor is just a fancy way to say that we will be passing a function to a value. (The suggestions that I've seen are passing values to a function.)

Create a TypeOf helper

const TypeOf = obj => Object.prototype.toString.call(obj).slice(8,-1);

This is similar to typeof, but it now returns Array for [] and Object for {}. I like to think of it as a strict typeof. If you're working on the Gmail application and performance is a concern then you can do something like this.

const TypeOf = obj => (
  Array.isArray(obj)
   ? "array"
    : obj === null // catch null edge case.  typeof null is an object :)
   ? null
    : typeof obj
)

You could stop here and call it a day. However, you could make it a bit more powerful using composition. You get a lot of benefits if you created a TypeBox Functor, again fancy word for passing a function to a value instead of passing a value to a function.

Create TypeBox

const TypeBox = (predicate, defaultValue) => {
  const TypePredicate = value => ({
     value,
     map: cb => predicate(value)
                ? TypePredicate(cb(value))
                : TypePredicate(defaultValue)
  });
  return TypePredicate;
}

There's a lot going on here, but it's very powerful. The TypeBox function uses a closure and returns our Functor. Closures give you access to Lexical_Scope. Think of it as a backpack that holds the things you want access to later.

Create ArrayBox

const ArrayBox = TypeOf(obj => TypeOf(obj) === 'Array' ? obj : [obj]);

ArrayBox is passing our predicate and defaultValue to TypeOf and will be available when we invoke/execute ArrayBox(name it whatever makes sense for your use case).

Now the fun part

If the input is an Array, return it.

ArrayBox(["foo", "bar"]).value; // ['foo', 'bar']

If the input is not an array, return it in one

ArrayBox("foo").value // ["foo"]

What's great about this approach is that it scales, is easy to test, and it uses composition. You can compose the functions in any manner to get the desired result.

There's many other ways we could approach this using Either or monads.

Solution 36 - Javascript

Other methods also exist to check, but I prefer the following method as my best way to check (as you can easily check types of other objects).

> a = [1, 2]
[ 1, 2 ]
>
> Object.prototype.toString.call(a).slice(8,).replace(/\]$/, '')
'Array'
>
> Object.prototype.toString.call([]).slice(8,-1) // best approach
'Array'

Explanation (with simple examples on Node REPL)»

> o = {'ok': 1}
{ ok: 1 }
> a = [1, 2]
[ 1, 2 ]
> typeof o
'object'
> typeof a
'object'
>
> Object.prototype.toString.call(o)
'[object Object]'
> Object.prototype.toString.call(a)
'[object Array]'
>

Object or Array »

> Object.prototype.toString.call(o).slice(8,).replace(/\]$/, '')
'Object'
>
> Object.prototype.toString.call(a).slice(8,).replace(/\]$/, '')
'Array'
>

Null or Undefined »

> Object.prototype.toString.call(undefined).slice(8,).replace(/\]$/, '')
'Undefined'
> Object.prototype.toString.call(null).slice(8,).replace(/\]$/, '')
'Null'
>

String »

> Object.prototype.toString.call('ok').slice(8,).replace(/\]$/, '')
'String'

Number »

> Object.prototype.toString.call(19).slice(8,).replace(/\]$/, '')
'Number'
> Object.prototype.toString.call(19.0).slice(8,).replace(/\]$/, '')
'Number'
> Object.prototype.toString.call(19.7).slice(8,).replace(/\]$/, '')
'Number'
>

I appreciate @mpen's suggestion to use -1 in place of regular expression as follows.

> Object.prototype.toString.call(12).slice(8,-1)
'Number'
>
> Object.prototype.toString.call(12.0).slice(8,-1)
'Number'
>
> Object.prototype.toString.call([]).slice(8,-1)
'Array'
> Object.prototype.toString.call({}).slice(8,-1)
'Object'
>
> Object.prototype.toString.call('').slice(8,-1)
'String'
>

Solution 37 - Javascript

I found the shortest answer now:

var x = [1,2,3]
console.log(x.map?1:0)

Solution 38 - Javascript

Here is a solution that I came up with and have been using for my projects...

function isArray (o) {
    return typeof o === "object" && o.length !== undefined;
}

isArray({}); // false
isArray(1); // false
isArray("str"); // false
isArray(function(){}); // false

isArray([]); // true

The only pitfall is that it will give a false positive if your object happens to have a length property:

isArray({length:0}); // true

If you are okay with that drawback and know your pure objects won't have that property, it's a clean solution and should be faster than the Object.prototype.toString.call method.

Solution 39 - Javascript

Use:

var is_array = function (value) {
   return value &&
     typeof value === 'object' &&
     typeof value.length === 'number' &&
     typeof value.splice === 'function' &&
    !(value.propertyIsEnumerable('length'));
};

This function has been taken from "JavaScript: The Good Parts" book, and it works perfect for me.

Solution 40 - Javascript

Since I don't like any Object.prototype-calls, I searched for another solution. Especially because the solutions of ChaosPandion won't always work, and the solution of MidnightTortoise with isArray() doesn't work with arrays coming from the DOM (like getElementsByTagName). And finally I found an easy and cross-browser solution, which probably also would have worked with Netscape 4. ;)

It's just these four lines (checking any object h):

function isArray(h){
    if((h.length!=undefined&&h[0]!=undefined)||(h.length===0&&h[0]===undefined)){
        return true;
    }
    else{ return false; }
}

I already tested these arrays (all return true):

1) array=d.getElementsByName('some_element'); //'some_element' can be a real or unreal element
2) array=[];
3) array=[10];
4) array=new Array();
5) array=new Array();
   array.push("whatever");

Does this work for all cases? Or is there a case where my solution doesn't work?

Solution 41 - Javascript

You can use this function to get the data type.

var myAr = [1,2];

checkType(myAr);

function checkType(data) {
  if(typeof data ==='object') {
    if(Object.prototype.toString.call(data).indexOf('Array') !== (-1)) {
      return 'array';
    } else {
      return 'object';
    }
  } else {
    return typeof data;
  }
}

if(checkType(myAr) === 'array') {
  console.log('yes, it is an array')
};

Solution 42 - Javascript

You can find with push like below:

function isArray(obj){ return (typeof obj.push === 'function') ? true : false; }

var array = new Array();

or

var array = ['a', 'b', 'c']; console.log(isArray(array));

Solution 43 - Javascript

There is a difference between checking out its prototype and Array.isArray:

function isArray(obj){
    return Object.getPrototypeOf(obj) === Array.prototype
}

This function will directly check if an obj is an array.

But for this Proxy object:

var arr = [1,2,3]

var proxy = new Proxy(arr,{})

console.log(Array.isArray(proxy)) // true

Array.isArray will take it as Array.

Solution 44 - Javascript

Here's a code snippet that'll explain an important fact of arrays that should be known early on while learning JavaScript (unlike me).

// this functions puts a string inside an array
var stringInsideArray = function(input) {
  if (typeof input === 'string') {
    return [input];
  }
  else if (Array.isArray(input)) {
    return input;
  }
  else {
    throw new Error("Input is not a string!");
  }
}

var output = stringInsideArray('hello');
console.log('step one output: ', output); // ["hello"]

// use typeof method to verify output is an object
console.log('step two output: ', typeof output); // object

// use Array.isArray() method to verify output is an array
console.log('step three output: ', Array.isArray(output)); // true

Arrays, are in fact, objects.

Using the typeof operator, the output of stringInsideArray('hello') proves that ["hello"] is really an object. This baffled me for the longest time because I assumed that arrays would be a JavaScript data type...

There are only seven JavaScript data types and arrays are not one of them.

To answer your question, using the Array.isArray() method determines that the output is an array.

Solution 45 - Javascript

Array.isArray is the way to go about this. For example:

var arr = ['tuna', 'chicken', 'pb&j'];
var obj = {sandwich: 'tuna', chips: 'cape cod'};

// Returns true
Array.isArray(arr);

// Return false
Array.isArray(obj);

Solution 46 - Javascript

First you can check console.log(typeof Object).

If the output is object then var {data}=object, i.e., just destructure the object according to the object keys.

And the function can be like this:

const abc = (str1, str2=null) => {
    var result = [];
    result.push(str1);
    result.push(str2);
    return result.join("");
}

Solution 47 - Javascript

// In simple ways

const arr = [1, 2, 3];
const obj = { message: 'nice' };
const str = 'nice';
const empty = null;

console.log(Array.isArray(arr));
console.log(Array.isArray(obj));
console.log(Array.isArray(str));
console.log(Array.isArray(empty));

Solution 48 - Javascript

Here's what I use:

function isArray(input) {
  if (input instanceof Array || Object.prototype.toString.call(input) === '[object Array]') {
        return true;
  } else return false;
}

Solution 49 - Javascript

You can also check with array's length property. When you will try to access the length property of an array, it will return a number (0 for an empty array) while if you try to access the length property of object it will return undefined.

if(Object.prototype.toString.call(arrayList) === '[object Array]') {
  console.log('Array!');
}

Solution 50 - Javascript

Array.isArray(obj) does not give very helpful results. I've created a prototype method of Object that seems to correctly determine whether and object is an array or not.

The only edge case that I know of where it fails is when item in the array is set to undefined.

Object.prototype.isArrayLike = function()
{
    var length = this.length || Object.keys(this).length;
    if (length === 0 || this.constructor.name === "String")
        return false;
    for (i = 0; i < length; i++)
    {
        if (typeof this[i] === "undefined")
            return false;
    }
    return true;
};

var arr = ['aaa', 'bbb', 'ccc', 'ddd'];
var arr1 = {"0":'aaa', "1":'bbb', 2:'ccc', 3:'ddd'};
var arr2 = {"0":'aaa', "a":'bbb', 2:'ccc', 3:'ddd'};
var arr3 = "qwerty";
var arr4 = [];
var arr5 = {0:'aaa', 1:'bbb', 2:'ccc', 3:'ddd'};

console.log("arrayLike:" + arr.isArrayLike());
console.log("Array.isArray(arr):" + Array.isArray(arr));
// arrayLike: true
// Array.isArray(arr): true
console.log("arrayLike1:" + arr1.isArrayLike());
console.log("Array.isArray(arr1):" + Array.isArray(arr1));
// arrayLike1: true
// Array.isArray(arr1): false
console.log("arrayLike2:" + arr2.isArrayLike());
console.log("Array.isArray(arr2):" + Array.isArray(arr2));
// arrayLike2: false
// Array.isArray(arr2): false
console.log("arrayLike3:" + arr3.isArrayLike());
console.log("Array.isArray(arr3):" + Array.isArray(arr3));
// arrayLike3: false
// Array.isArray(arr3): false
console.log("arrayLike4:" + arr4.isArrayLike());
console.log("Array.isArray(arr4):" + Array.isArray(arr4));
// arrayLike4: false
// Array.isArray(arr4): true
console.log("arrayLike5:" + arr5.isArrayLike());
console.log("Array.isArray(arr5):" + Array.isArray(arr5));
// arrayLike5: false
// Array.isArray(arr5): true

Solution 51 - Javascript

 var length = 16;                               // Number
 var lastName = "Johnson";                      // String
 var cars = ["Saab", "Volvo", "BMW"];           // Array
 var x = {firstName:"John", lastName:"Doe"};
 
 Object.prototype.myCheck= function(){
 if (this.constructor === Array){
          alert('array');
        }else if (this.constructor === Object)
       {
         alert('object');
        }else if (this.constructor === Number)
        {
          alert('number');
        }else if (this.constructor === String)
        {
          alert('string');
        }

 }
 cars.myCheck();
 lastName.myCheck();
 length.myCheck();

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmpenView Question on Stackoverflow
Solution 1 - Javascriptuser113716View Answer on Stackoverflow
Solution 2 - JavascriptFelaView Answer on Stackoverflow
Solution 3 - JavascriptChaosPandionView Answer on Stackoverflow
Solution 4 - JavascriptjanrView Answer on Stackoverflow
Solution 5 - JavascriptshinobiView Answer on Stackoverflow
Solution 6 - JavascriptAlirezaView Answer on Stackoverflow
Solution 7 - JavascriptCruorVultView Answer on Stackoverflow
Solution 8 - JavascriptMidnightTortoiseView Answer on Stackoverflow
Solution 9 - Javascriptajax333221View Answer on Stackoverflow
Solution 10 - JavascriptVikash KumarView Answer on Stackoverflow
Solution 11 - JavascriptSafareliView Answer on Stackoverflow
Solution 12 - JavascriptBilly MoonView Answer on Stackoverflow
Solution 13 - JavascriptAhmet DALView Answer on Stackoverflow
Solution 14 - JavascriptrsbkkView Answer on Stackoverflow
Solution 15 - JavascriptDexygenView Answer on Stackoverflow
Solution 16 - JavascriptEugeneView Answer on Stackoverflow
Solution 17 - Javascriptle_topView Answer on Stackoverflow
Solution 18 - JavascriptAtishay JainView Answer on Stackoverflow
Solution 19 - JavascriptJohn WundesView Answer on Stackoverflow
Solution 20 - JavascriptTim DownView Answer on Stackoverflow
Solution 21 - JavascriptnamuolView Answer on Stackoverflow
Solution 22 - JavascriptSalvador DaliView Answer on Stackoverflow
Solution 23 - JavascriptyesilView Answer on Stackoverflow
Solution 24 - JavascriptmpenView Answer on Stackoverflow
Solution 25 - JavascriptSTEELView Answer on Stackoverflow
Solution 26 - JavascriptAlauddin Afif CassandraView Answer on Stackoverflow
Solution 27 - JavascriptexebookView Answer on Stackoverflow
Solution 28 - JavascriptRoboTamerView Answer on Stackoverflow
Solution 29 - JavascriptBrad ParksView Answer on Stackoverflow
Solution 30 - JavascriptVIJAY PView Answer on Stackoverflow
Solution 31 - JavascriptSheelpriyView Answer on Stackoverflow
Solution 32 - JavascriptkolyasegView Answer on Stackoverflow
Solution 33 - JavascriptKamil KiełczewskiView Answer on Stackoverflow
Solution 34 - JavascriptBraden BestView Answer on Stackoverflow
Solution 35 - JavascriptRobby_robView Answer on Stackoverflow
Solution 36 - JavascripthygullView Answer on Stackoverflow
Solution 37 - JavascriptLuis felipe De jesus MunozView Answer on Stackoverflow
Solution 38 - JavascriptSensei_ShohView Answer on Stackoverflow
Solution 39 - Javascriptuser3367643View Answer on Stackoverflow
Solution 40 - JavascriptMarcusView Answer on Stackoverflow
Solution 41 - JavascriptKamuran SönecekView Answer on Stackoverflow
Solution 42 - JavascriptlalithkumarView Answer on Stackoverflow
Solution 43 - JavascriptbytefishView Answer on Stackoverflow
Solution 44 - JavascriptunderthecodeView Answer on Stackoverflow
Solution 45 - JavascriptbijayshresthaView Answer on Stackoverflow
Solution 46 - JavascriptSouvik DeyView Answer on Stackoverflow
Solution 47 - JavascriptForce BoltView Answer on Stackoverflow
Solution 48 - JavascriptRic FlairView Answer on Stackoverflow
Solution 49 - JavascriptPartha Sarathi NandaView Answer on Stackoverflow
Solution 50 - JavascriptDan BrayView Answer on Stackoverflow
Solution 51 - JavascriptGauravView Answer on Stackoverflow