Introduction
Background
In this article, as a beginner, you’ll be comfortable as long as you are familiar with objects in JavaScript. However, if you have advanced experience dealing with arrays or objects in JavaScript, you’ll quickly grasp this concept, and I hope you can give some comments below.
How to Check If a Variable is an Array?
Why is an Array a Special Type of Object?
Suppose you have been using JavaScript for a while now. You’ll probably agree that a JavaScript array is a container of any type and numerically indexed. These can be any type, such as boolean, number, string, object, or even an array called a multidimensional array.
JavaScript arrays are objects. If you execute the code below, it will return an object as its type.
- const pizza = [];
-
- console.log(typeof(pizza));
OK, that’s weird. If an array is an object; therefore, JavaScript arrays can have string keys/properties added to them. The answer is yes. However, the string keys/properties don’t add up to the length property of the array. Let’s see an example below.
- const pizza = ['pepperoni','double cheese pizza', 'hawaiian'];
-
-
- console.log('Array of pizza has a length of ' + pizza.length);
-
-
- pizza["hasOnions"] = true;
-
-
- console.log('Array of pizza has a length of '+ pizza.length + ' after adding a new string property');
-
-
- console.log(pizza[3]);
-
-
- console.log(pizza);
Another confusing feature about array. If a string value is equivalent to an integer/standard base-10 number, then JavaScript assumes that you wanted to use it as a number index rather than a string key/property.
- techWriters["10"] = "Mr. Jin Vincent Necesario";
-
-
- console.log(techWriters);
-
-
If you have seen the sample code above, it is tricky. Hence, this practice isn’t advisable.
Using the Object.prototype.toString.call
- Another alternative to typeof operator is Object.prototype.toString.call() method.
- This method returns a string that represents the current object, e.g., Function, Array, etc.
- However, let’s not forget to use the expression !==null, as discussed in the previous example.
Let us see the examples below.
- const customer = {};
-
-
-
- console.log(Object.prototype.toString.call(customer) === '[object Object]');
-
-
-
- console.log(Object.prototype.toString.call(customer).slice(8, -1) === 'Object');
-
-
-
- console.log((Object.prototype.toString.call(customer) === '[object Object]') && customer !==null);
-
-
-
- console.log((Object.prototype.toString.call(customer).slice(8, -1) === 'Object') && customer !==null);
The difference between the typeof operator and Object.prototype.toString.call
- The typeof operator returns the type of object in string format. Moreover, you can’t overload this operator.
- The Object.prototype.toString.call() method returns the string representation of the object. Moreover, you can override it to return anything based on your needs.
Remarks
Thank goodness you got this far. As you can see, you have a lot of options for what to use when checking a variable if it is an array or an object. Of course, the decision is all up to you. Hopefully, this post has given you enough practical knowledge and makes you confident when deciding what, when, and how to use these methods and operators in a given situation.
Summary
This post has started on checking if a variable is an array and given some code samples. We have seen how to check if a variable is an object and given some code samples.
I hope you have enjoyed this article, as I have enjoyed writing it. Stay tuned for more. This article was originally written and posted
here. Until next time, happy programming!