But not a problem! Use Array.isArray() โ€“ finally, there is an easier way to check if a value is an actual array ๐ŸŽ‰

const books = ['๐Ÿ“•', '๐Ÿ“™', '๐Ÿ“—'];

// Old way
Object.prototype.toString.call(books) === '[object Array]';

// โœ… Better
Array.isArray(books);

Array is not a true array

Letโ€™s see what I mean by this, array is not a true array.

const array = [];

typeof array; // 'object'

โ˜๏ธThatโ€™s why you canโ€™t use your typical typeof. Because array is an object type ๐Ÿ˜•

Array.isArray Demo

Alright, letโ€™s try this method on other values and see what we get ๐Ÿ‘ฉโ€๐Ÿ”ฌ

These are all arrays, and will return true

// Empty Array
Array.isArray([]); // true

// Array
Array.isArray(['๐Ÿ““']); // true

// Array Constructor
Array.isArray(new Array('๐Ÿ““')); // true

These are NOT arrays and will return false

// Object
Array.isArray({}); // false

// Object
Array.isArray({book: '๐Ÿ““'}); // false

// Number
Array.isArray(123); // false

// Boolean
Array.isArray(true); // false

// Boolean
Array.isArray(false); // false

// String
Array.isArray('hello'); // false

// Null
Array.isArray(null); // false

// Undefined
Array.isArray(undefined); // false

// NaN
Array.isArray(NaN); // false

instanceof vs Array.isArray

Another popular choice you might is using instanceof

const books = ['๐Ÿ“•', '๐Ÿ“™', '๐Ÿ“—'];

books instanceof Array; // true

Butโ€ฆ

The problem is it doesnโ€™t work with multiple context (e.g. frames or windows). Because each frame has different scopes with its own execution environment. Thus, it has a different global object and different constructors. So if you try to test an array against that frameโ€™s context, it will NOT return true, it will return incorrectly as false.

๐Ÿคฏ What are you talking about??? ๐Ÿ‘ˆ If this is floating in your mind. Donโ€™t worry, I was too. To understand this, you need to understand JavaScriptโ€™s execution context. Hereโ€™s a great video explaining it, An Introduction to Functions, Execution Context and the Call Stack. This is a bit more of an advanced topic, so if youโ€™re just a beginner, feel free to skip through it. And when you get a bit more comfortable with JavaScript, then definitely return to this topic. In the meantime, let me try to explain this โ€œmultiple contextโ€ in non-dev terms.

Explanation in non-dev terms

You can think of frames like different planets. Every planet has its own system, with different gravity pull and composition. So instanceof only works on our planet, Earth. If you bring it to Mars, it wonโ€™t work. However, with Array.isArray() it will work on any planet. Itโ€™s universal. Thatโ€™s why you should use Array.isArray().

// Creating our new "planet" called mars
const mars = document.createElement('iframe');
document.body.appendChild(iframe);
xArray = window.frames[window.frames.length-1].Array;

// Let's make an array in our new "planet", mars
var marsArray = new xArray('๐Ÿ‘ฉ', '๐Ÿ‘จ');

// Using the instanceof tool to test the marsArray
marsArray instanceof Array;
//  false --> โŒ doesn't work

// Now, let's try using our universal tool
Array.isArray(marsArray)
// true --> โœ… great, it works!

Thanks for reading โค

If you liked this post, please do share/like it with all of your programming buddies!

Follow us on Facebook | Twitter

Further reading

โ˜ž The Complete JavaScript Course 2019: Build Real Projects!

โ˜ž Vue JS 2 - The Complete Guide (incl. Vue Router & Vuex)

โ˜ž JavaScript Bootcamp - Build Real World Applications

โ˜ž The Web Developer Bootcamp

โ˜ž New ES2019 Features Every JavaScript Developer Should Know

โ˜ž Best JavaScript Frameworks, Libraries and Tools to Use in 2019

โ˜ž 12 tips for writing clean and scalable JavaScript

โ˜ž 10 JavaScript Charting Libraries for Every Data Visualization Need

#javascript

Better Array check with Array.isArray ๐ŸŽ‰๐ŸŽ‰๐ŸŽ‰
41.70 GEEK