Pattern. Common for “value or values” function args.
Compatibility
Browser & Runtime Support
_.castArray is a Lodash 4.x Lang helper. It runs anywhere Lodash runs — modern browsers (CDN or bundle) and Node.js / Bun / Deno with npm install lodash.
✓ Stable · Lodash 4.x
_.castArray()
Available in [email protected] full builds and the lodash/castArray modular import. Closest plain-JS pattern: Array.isArray(v) ? v : (arguments.length ? [v] : []).
100%With Lodash 4.x
Google ChromeSupported · Desktop & Mobile
Full support
Mozilla FirefoxSupported · Desktop & Mobile
Full support
Apple SafariSupported · macOS & iOS
Full support
Microsoft EdgeSupported · Chromium
Full support
Internet ExplorerNo native support · Use a polyfill
Polyfill
OperaSupported · Modern versions
Full support
Samsung InternetSupported · Android
Full support
BunSupported · JavaScript runtime
Supported
DenoSupported · JavaScript runtime
Supported
Node.jsSupported · Server runtime
Supported
Android WebViewSupported · Modern WebView
Full support
_.castArrayLodash 4.x+
Bottom line: Safe for production once Lodash is loaded. Prefer import castArray from "lodash/castArray" for smaller bundles. Use it whenever APIs accept a scalar or an array of the same type.
Frequently Asked Questions
_.castArray(value) returns value unchanged if it is already an array; otherwise it returns a new one-element array containing value.
No. Primitives and objects are not mutated. If the input is already an array, the same array reference is returned.
An empty array []. Handy when an optional value should always become a safe list.
Wrapping with [value] always creates a new array — even if value is already an array, you get a nested list. castArray avoids that double wrap.
toArray converts array-likes and objects into arrays of elements. castArray only guarantees an array container around a single value (or returns an existing array).
Use import castArray from "lodash/castArray" or require("lodash/castArray") for tree-shaking friendly bundles. Full build: import _ from "lodash" then _.castArray.