Files
30-seconds-of-code/js/snippets/cast-array.md
2023-05-03 20:39:23 +03:00

434 B

title, type, tags, cover, dateModified
title type tags cover dateModified
Cast to array snippet
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]