Skip to content

Commit a5eee58

Browse files
committed
Add Check If Something Is An Array as a javascript til
1 parent 1f8bc17 commit a5eee58

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
1010
For a steady stream of TILs from a variety of rocketeers, checkout
1111
[til.hashrocket.com](https://til.hashrocket.com/).
1212

13-
_669 TILs and counting..._
13+
_670 TILs and counting..._
1414

1515
---
1616

@@ -227,6 +227,7 @@ _669 TILs and counting..._
227227

228228
- [Accessing Arguments To A Function](javascript/accessing-arguments-to-a-function.md)
229229
- [Character Codes from Keyboard Listeners](javascript/character-codes-from-keyboard-listeners.md)
230+
- [Check If Something Is An Array](javascript/check-if-something-is-an-array.md)
230231
- [Computed Property Names In ES6](javascript/computed-property-names-in-es6.md)
231232
- [Create An Array Containing 1 To N](javascript/create-an-array-containing-1-to-n.md)
232233
- [Create Bootstrapped Apps With Yarn](javascript/create-bootstrapped-apps-with-yarn.md)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Check If Something Is An Array
2+
3+
The `Array` class has a function on it called
4+
[`isArray()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)
5+
which can be used to check if something is an array.
6+
7+
```javascript
8+
> Array.isArray('Hello, World!');
9+
// => false
10+
11+
> Array.isArray(['One', 2, [3]]);
12+
// => true
13+
14+
> Array.isArray({ foo: 'bar' });
15+
// => false
16+
17+
> Array.isArray([]);
18+
// => true
19+
```
20+
21+
The MDN docs provide an [example
22+
polyfill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray#Polyfill)
23+
if it is not natively available.
24+
25+
```javascript
26+
if (!Array.isArray) {
27+
Array.isArray = function(arg) {
28+
return Object.prototype.toString.call(arg) === '[object Array]';
29+
};
30+
}
31+
```

0 commit comments

Comments
 (0)