30-seconds-of-code
Version:
A collection of useful JavaScript snippets.
16 lines (11 loc) • 410 B
Markdown
Checks if the provided argument is array-like (i.e. is iterable).
Check if the provided argument is not `null` and that its `Symbol.iterator` property is a function.
```js
const isArrayLike = obj => obj != null && typeof obj[Symbol.iterator] === 'function';
```
```js
isArrayLike(document.querySelectorAll('.className')); // true
isArrayLike('abc'); // true
isArrayLike(null); // false
```