Files
30-seconds-of-code/snippets/js/s/cast-array.md
2023-05-07 16:07:29 +03:00

455 B

title, type, language, tags, cover, dateModified
title type language tags cover dateModified
Cast to array snippet javascript
type
array
man-red-sunset 2020-09-15T16:28:04+03:00

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

  • Use Array.isArray() to determine if val is an array and return it as-is or encapsulated in an array accordingly.
const castArray = val => (Array.isArray(val) ? val : [val]);
castArray('foo'); // ['foo']
castArray([1]); // [1]