mirror of
https://github.com/jbranchaud/til
synced 2026-01-07 09:08:01 +00:00
Add Check If Something Is An Array as a javascript til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||||
|
|
||||||
_669 TILs and counting..._
|
_670 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -227,6 +227,7 @@ _669 TILs and counting..._
|
|||||||
|
|
||||||
- [Accessing Arguments To A Function](javascript/accessing-arguments-to-a-function.md)
|
- [Accessing Arguments To A Function](javascript/accessing-arguments-to-a-function.md)
|
||||||
- [Character Codes from Keyboard Listeners](javascript/character-codes-from-keyboard-listeners.md)
|
- [Character Codes from Keyboard Listeners](javascript/character-codes-from-keyboard-listeners.md)
|
||||||
|
- [Check If Something Is An Array](javascript/check-if-something-is-an-array.md)
|
||||||
- [Computed Property Names In ES6](javascript/computed-property-names-in-es6.md)
|
- [Computed Property Names In ES6](javascript/computed-property-names-in-es6.md)
|
||||||
- [Create An Array Containing 1 To N](javascript/create-an-array-containing-1-to-n.md)
|
- [Create An Array Containing 1 To N](javascript/create-an-array-containing-1-to-n.md)
|
||||||
- [Create Bootstrapped Apps With Yarn](javascript/create-bootstrapped-apps-with-yarn.md)
|
- [Create Bootstrapped Apps With Yarn](javascript/create-bootstrapped-apps-with-yarn.md)
|
||||||
|
|||||||
31
javascript/check-if-something-is-an-array.md
Normal file
31
javascript/check-if-something-is-an-array.md
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
# Check If Something Is An Array
|
||||||
|
|
||||||
|
The `Array` class has a function on it called
|
||||||
|
[`isArray()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray)
|
||||||
|
which can be used to check if something is an array.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
> Array.isArray('Hello, World!');
|
||||||
|
// => false
|
||||||
|
|
||||||
|
> Array.isArray(['One', 2, [3]]);
|
||||||
|
// => true
|
||||||
|
|
||||||
|
> Array.isArray({ foo: 'bar' });
|
||||||
|
// => false
|
||||||
|
|
||||||
|
> Array.isArray([]);
|
||||||
|
// => true
|
||||||
|
```
|
||||||
|
|
||||||
|
The MDN docs provide an [example
|
||||||
|
polyfill](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray#Polyfill)
|
||||||
|
if it is not natively available.
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
if (!Array.isArray) {
|
||||||
|
Array.isArray = function(arg) {
|
||||||
|
return Object.prototype.toString.call(arg) === '[object Array]';
|
||||||
|
};
|
||||||
|
}
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user