I wrote a utility function for my nextjs project. In there, I got something that I unexpected. I initialised an empty array which be filled with the data later. When I type check that empty array, it shows an object. I don't want an object. I want an array. Why? Could someone told me why does it happen.
Asked
Active
Viewed 183 times
0
-
4In JS, arrays are objects. `Arrays are list-like objects whose prototype has methods to perform traversal and mutation operations.` -MDN – DecPK Aug 25 '21 at 02:05
1 Answers
1
Yes typeof array is an object in javascript
if you want to check one variable is an object or array then you can use
Array.isArray()
console.log(Array.isArray(arr)) // true
console.log(Array.isArray(obj)) // false

Ashirbad Panigrahi
- 634
- 6
- 13