From 4c6182775d1012427485aa6036535375033e3433 Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Thu, 14 Dec 2017 13:10:06 +0200 Subject: [PATCH] Add isArray --- README.md | 12 ++++++++++++ snippets/is-array.md | 9 +++++++++ 2 files changed, 21 insertions(+) create mode 100644 snippets/is-array.md diff --git a/README.md b/README.md index 7e3df18b7..b838a8f6c 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ * [Initial of list](#initial-of-list) * [Initialize array with range](#initialize-array-with-range) * [Initialize array with values](#initialize-array-with-values) +* [Is array](#is-array) * [Is boolean](#is-boolean) * [Is function](#is-function) * [Is number](#is-number) @@ -538,6 +539,17 @@ const initializeArray = (n, value = 0) => Array(n).fill(value); // initializeArray(5, 2) -> [2,2,2,2,2] ``` +[⬆ back to top](#table-of-contents) +### Is array + +Use `Array.isArray()` to check if a value is classified as an array. + +```js +const isArray = val => val && Array.isArray(val); +// isArray(null) -> false +// isArray([1]) -> true +``` + [⬆ back to top](#table-of-contents) ### Is boolean diff --git a/snippets/is-array.md b/snippets/is-array.md new file mode 100644 index 000000000..7652f74c9 --- /dev/null +++ b/snippets/is-array.md @@ -0,0 +1,9 @@ +### Is array + +Use `Array.isArray()` to check if a value is classified as an array. + +```js +const isArray = val => val && Array.isArray(val); +// isArray(null) -> false +// isArray([1]) -> true +```