Lodash _.castArray() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.castArray(value) ensures your logic always receives an array.
  • Why existing arrays are returned directly instead of being wrapped again.
  • How null, undefined, objects, and strings behave with castArray.
  • When castArray is better than manual [value] wrapping.

Prerequisites

Skim Lodash Lang methods first so you can compare castArray with nearby converters like toArray.

  • You understand basic JavaScript values (primitive, object, array, nullish).
  • You can run snippets in Node or open the Try-it labs in a browser.

Overview

_.castArray is a small normalization helper. It guarantees an array output shape so mapping, iteration, and validation code can use one consistent branch.

Single output shape

Great for APIs that accept one or many values; your downstream code always receives an array.

No double wrapping

Arrays are returned directly, so [1,2] stays [1,2] instead of [[1,2]].

Predictable nullish handling

_.castArray(null) becomes [null] and empty call returns [].

Syntax

javascript
_.castArray(value)
  • value: any JavaScript value (optional).
  • Returns: an array. If input is already an array, the same reference is returned.
1

Wrap a single value

Non-array values are wrapped into a one-item array so downstream loops can stay consistent.

javascript
import castArray from "lodash/castArray";

castArray(42);       // [42]
castArray("hello");  // ["hello"]
Try it Yourself
2

Already an array

If the input is an array, castArray returns it unchanged, avoiding nested array output.

javascript
import castArray from "lodash/castArray";

const nums = [1, 2, 3];
const result = castArray(nums);

console.log(result === nums); // true
Try it Yourself
3

Null and undefined behavior

null becomes [null], and no-argument call returns an empty array.

javascript
import castArray from "lodash/castArray";

castArray(null); // [null]
castArray();     // []
Try it Yourself

📋 castArray vs alternatives

ApproachArray inputScalar input
_.castArray(value)Returns same arrayWraps into single-item array
[value]Creates nested array ([[...]])Wraps into single-item array
_.toArray(value)Converts/copies elementsMay split strings or return object values

Pitfalls to avoid

Wrap

Using [value] blindly

Manual wrapping can produce nested arrays when input is already an array. Prefer castArray when shape is unknown.

Intent

Confusing castArray with toArray

castArray normalizes container shape; toArray converts/splits values into elements.

Refs

Assuming a cloned array

Existing arrays are returned by reference. Clone separately if you plan to mutate and need isolation.

❓ FAQ

No. It does not mutate primitives or objects. If the input is already an array, it returns that same array reference.
It returns an empty array []. This is useful for optional inputs where you want a safe iterable.
Wrapping always creates a new array, even when value is already an array. castArray avoids double-wrapping and preserves existing arrays.
Use toArray when converting array-like values or objects into arrays of elements. Use castArray when you only need to guarantee an array container.

Summary

  • Purpose: _.castArray standardizes unknown input into an array shape.
  • Behavior: it preserves existing arrays and wraps non-arrays.
  • Next: continue to Lodash _.clone().
Did you know?

_.castArray(value) returns value as-is when it is already an array, so no extra wrapper array is created.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful