Skip to main content
This release is 5 versions behind 1.33.0 — the latest version of @es-toolkit/es-toolkit. Jump to latest

@es-toolkit/es-toolkit@1.29.0-dev.958+1d1d0ee6
Built and signed on GitHub Actions

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
3 months ago (1.29.0-dev.958+1d1d0ee6)
function castArray
castArray<T>(value?: T | readonly T[]): T[]

Casts value as an array if it's not one.

Examples

const arr1 = castArray(1); // Returns: [1]

const arr2 = castArray([1]); // Returns: [1]

const arr3 = castArray({'a': 1}); // Returns: [{'a': 1}]

const arr4 = castArray(null); // Returns: [null]

const arr5 = castArray(undefined); // Returns: [undefined]

const arr6 = castArray(); // Returns: []

Type Parameters

The type of elements in the array.

Parameters

optional
value: T | readonly T[]

The value to be cast to an array.

Return Type

T[]

An array containing the input value if it wasn't an array, or the original array if it was.

Add Package

deno add jsr:@es-toolkit/es-toolkit

Import symbol

import { castArray } from "@es-toolkit/es-toolkit/compat";

---- OR ----

Import directly with a jsr specifier

import { castArray } from "jsr:@es-toolkit/es-toolkit/compat";

Add Package

npx jsr add @es-toolkit/es-toolkit

Import symbol

import { castArray } from "@es-toolkit/es-toolkit/compat";

Add Package

yarn dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { castArray } from "@es-toolkit/es-toolkit/compat";

Add Package

pnpm dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { castArray } from "@es-toolkit/es-toolkit/compat";

Add Package

bunx jsr add @es-toolkit/es-toolkit

Import symbol

import { castArray } from "@es-toolkit/es-toolkit/compat";