-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfind-array-or-not.js
34 lines (26 loc) · 928 Bytes
/
find-array-or-not.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*
Find array or not: if you noticed, in javascript `typeof` will give object for an [1,2,3]. Because JS treats array as object in a way.
Multiple ways to find a variable is array or not:
1. checking a variable is instanceof Array
2. comparing variable.constructor is an Array
3. using Array.isArray();
Reference:
1. http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript
*/
// Example 1
var arr = [1];
console.log(arr instanceof Array); // true
var arr = {};
console.log(arr instanceof Array); // false
// Example 2
var arr = [1];
console.log(arr.constructor === Array); // true
var arr = {};
console.log(arr.constructor === Array); // false
console.log(arr.constructor === Object); // true
// Example 3
var arr = [1];
console.log(Array.isArray(arr)); // true
// Example 4
var arr = [1];
console.log(Object.prototype.toString.call(arr) === "[object Array]"); // true