Syntax, Parameter and Return Value
Syntax:
_.isArray(value)
Parameters:
value: The value to check.
Return Value:
(boolean) - Returns true if value is an array, else false.
Examples
Basic Array Check:
JavaScript
var _ = require('lodash');
console.log(_.isArray([1, 2, 3]));
console.log(_.isArray({ a: 1, b: 2 }));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
false
Demonstrates the basic usage of _.isArray() for identifying arrays.
Conditionally Processing Data:
function processData(data) {
if (_.isArray(data)) {
// Handle array-specific logic
} else {
// Handle non-array data
}
}
Shows conditional data processing based on whether the input is an array.
Using in Combination with Other Lodash Methods:
JavaScript
var data = [1, 2, 3];
if (_.isArray(data)) {
var doubled = _.map(data, x => x * 2);
console.log(doubled);
}

You can also try this code with Online Javascript Compiler
Run Code
Output:
[2, 4, 6]
An example of using _.isArray() before applying array-specific operations like mapping.
Filtering Arrays in a Collection:
JavaScript
var mixedCollection = [1, 'string', [2, 3], { a: 4 }];
var arrays = _.filter(mixedCollection, _.isArray);
console.log(arrays);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[[2, 3]]
Demonstrates filtering out arrays from a mixed collection of items.
Frequently Asked Questions
How is _.isArray() different from JavaScript's Array.isArray()?
_.isArray() and JavaScript's native Array.isArray() function similarly. The primary difference is that _.isArray() is part of the Lodash library, which provides consistency and compatibility across various environments and versions of JavaScript.
Can _.isArray() detect array-like objects?
No, _.isArray() specifically checks for true arrays. Array-like objects (e.g., arguments object, NodeList) return false.
Is _.isArray() necessary with modern JavaScript?
While modern JavaScript provides Array.isArray(), using _.isArray() can be beneficial for consistency when using Lodash for other utilities, or in environments where Array.isArray() is not available or reliable.
Conclusion
Lodash's _.isArray() method provides a straightforward and reliable way to check for arrays in JavaScript. It's a useful utility for ensuring correct handling of data structures, particularly when dealing with complex collections or mixed data types.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.