Introduction
Hi! Fellow developers, once again, here is another JavaScript article. I often hang out at C# Corner to contribute and read articles. While browsing, I saw this post: “How to check if a JavaScript variable is an array or an object?”. As a result, I decided to create an article regarding it and expand a bit more.
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 = [];
- //output: object
- console.log(typeof(pizza));
- const pizza = ['pepperoni','double cheese pizza', 'hawaiian'];
- //output: Array of pizza has a length of 3
- console.log('Array of pizza has a length of ' + pizza.length);
- //add a new string property to the array
- pizza["hasOnions"] = true;
- //output: Array of pizza has a length of 3 after adding a new string property
- console.log('Array of pizza has a length of '+ pizza.length + ' after adding a new string property');
- //output: undefined
- console.log(pizza[3]);
- //output: [ 'pepperoni', 'double cheese pizza', 'hawaiian', hasOnions: true ]
- console.log(pizza);
- techWriters["10"] = "Mr. Jin Vincent Necesario";
- //output: (11) [empty × 10, "Mr. Jin Vincent Necesario"]
- console.log(techWriters);
- //output: 11


Join the conversation! Your thoughts help the community grow.