3 Ways to differentiate between Object, Array in JavaScript

In JavaScript Programming Language objects are quite broad level meaning they encapsulate a lot of other identities meaning their are a number of other identities in javaScript which can object as well as something else. On of the most confusing example would be of Array, Objects.

Every JavaScript array is an object but every object is not an array.

There are a number of small concepts associated with Objects, Arrays in JavaScript. If in case your not aware of these little aspects then please don’t read any further, just go to below links first to get a clear idea about What are JavaScript Objects, Arrays? and then come back to read this.

Using .length for differentiating between Array vs Object in JavaScript

JavaScript arrays does have length although it can be zero in some cases, but Objects generally don’t have length. But length of an object can be defined explicitly by programmer. That’s why .length is not goto metric for testing whether object is an array or not.

For example
var x = [ 37, 39, 72 ];
x.length; // Returns 3

var obj = { website: “Computer Science Hub” };
obj.length; // Returns undefined

But as I said Developer can explicitly define length as a property of object. So we could have something like
var obj = { website: “Computer Science Hub”, length : 20 }; obj.length; // Returns 20

Now it would be difficult to distinguish between obj and x and kind of tricky to say which one is Object/Array as both have length.

Using instanceof() method for distinguishing Object vs Array

instanceof method in JavaScript tells about from which class an object is created. Syntx for this method is
object_name instanceof class_name;

Here class_name need to be defined by developer if object_name is an object made from class_name then instanceof will return true otherwise false.

So for testing whether an object is an array or not, you just need to do something as following –
• object_name instanceof Array; // If true object_name is array

For Example
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
const auto = new Car(‘Honda’, ‘Accord’, 1998);
console.log(auto instanceof Car);
// Returns true

Using isArray() method for checking whether object is Array or not

JavaScript’s Array class have an inbuilt method isArray() which returns true or false depending upon whether argument passed to isArray() is actually an array or not.

Syntax
Array.isArray(argument);
// Returns true if argument is actually an array false otherwise

Conclusion – Ultimate approach to test whether an Object is array or not in JavaScript

I have list three approaches above for testing whether an object is an array or not.

  • Using .length
  • Using instanceof() method
  • Using isArray() method

But for saying with 100% certainty whether an object is an array or not. We can use these three methods together as well. That’s what I mostly do when I work on developing some applications using JavaScript.

var x = [ 10, 93, 29, 93 ];

if (x.isArray()) {
	if (x instanceof Array){
		if (x.length){
			console.log("x is an array");
		}
	}
}
else {
	console.log("x is not an array");
}

In the above example – I’ve used if else conditionals in case your not aware of what this is? Please check out JavaScript Conditionals which I wrote couple of days ago on this website.

Moreover if your learning JavaScript Programming Language then you can check out other articles about it here – JavaScript Computer Science Hub.

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts