Categories
JavaScript Answers

How to Check if an Object is not an Array in JavaScript?

Spread the love

Sometimes, we want to check if an object is not an array in JavaScript

In this article, we’ll look at how to check if an object is not an array in JavaScript.

Check if an Object is not an Array in JavaScript

To check if an object is not an array in JavaScript, we can use the Array.isArray method.

For instance, we write:

const arr = [1, 2, 3];
console.log(!Array.isArray(arr));

const obj = {}
console.log(!Array.isArray(obj));

We call Array.isArray with arr as its argument to see if arr is an array.

Then we negate the returned value to check if it’s not an array.

Since arr is an array, the console log should log false .

Then we call Array.isArray with obj as its argument to see if obj is an array.

Then we negate the returned value to check if it’s not an array.

Since obj is not an array, the console log should log true.

Conclusion

To check if an object is not an array in JavaScript, we can use the Array.isArray method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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