Determine if object is array in JavaScript/jQuery
This post will discuss how to determine whether an object is an array in JavaScript and jQuery.
The arrays in JavaScript can be created using the Array() constructor or Array literal notation. To determine whether a given object is an array, we can use any of the following functions in JavaScript:
1. Using Array.isArray() function
The standard function to determine whether the specified object is an array is with the Array.isArray() function, which is a built-in function that returns true if the given value is an array, and false otherwise.
|
1 2 3 4 5 6 7 8 |
let obj = [ { name: 'Max', age: 23 }, { name: 'John', age: 20 }, { name: 'Caley', age: 18 } ]; let isArray = Array.isArray(obj) console.log(isArray); // true |
2. Using Object.prototype.toString() function
Another option is to use call the Object.prototype.toString() function, which is a built-in function that returns a string representation of the object’s type. We can use this function to compare the object’s type with the string "[object Array]", which is the standard type for arrays. The approach is used internally by the Array.isArray() function. For instance:
|
1 2 3 4 5 6 7 8 |
let obj = [ { name: 'Max', age: 23 }, { name: 'John', age: 20 }, { name: 'Caley', age: 18 } ]; let isArray = Object.prototype.toString.call(obj) === '[object Array]'; console.log(isArray); // true |
3. Using instanceof operator
We can also use the instanceof operator, which is a built-in operator that tests if an object is an instance of a constructor function. We can use this operator to check if an object is an instance of the Array constructor function, which creates array objects.
|
1 2 3 4 5 6 7 8 |
let obj = [ { name: 'Max', age: 23 }, { name: 'John', age: 20 }, { name: 'Caley', age: 18 } ]; let isArray = obj instanceof Array; console.log(isArray); // true |
Alternatively, we can check if the value of the object constructor is the same as the standard built-in Array constructor. The !! operator should be used first to check if the object is truthy.
|
1 2 3 4 5 6 7 8 |
let obj = [ { name: 'Max', age: 23 }, { name: 'John', age: 20 }, { name: 'Caley', age: 18 } ]; let isArray = !!obj && obj.constructor === Array; console.log(isArray); // true |
4. Using Underscore/Lodash Library
We can use the _.isArray() function from the Underscore/Lodash library, which returns true if the given value is an array, and false otherwise. This function offers the same functionality as the Array.isArray() function.
|
1 2 3 4 5 6 7 8 9 10 11 |
// import Underscore module let _ = require('underscore'); // or lodash let obj = [ { name: 'Max', age: 23 }, { name: 'John', age: 20 }, { name: 'Caley', age: 18 } ]; let isArray = _.isArray(obj) console.log(isArray); // true |
5. Using jQuery library
With jQuery library, we can use the jQuery.isArray() function to check whether the given object is a JavaScript array. It returns true if the given value is an array, and false otherwise.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// import jquery library const { JSDOM } = require("jsdom"); const { window } = new JSDOM(); let $ = require("jquery")(window); let obj = [ { name: 'Max', age: 23 }, { name: 'John', age: 20 }, { name: 'Caley', age: 18 } ]; let isArray = $.isArray(obj) console.log(isArray); // true |
That’s all about checking if an object is an array in JavaScript and jQuery.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)