Basic Data Structures: Iterate Through All an Array's Items Using For Loops - newArr type is Object

I’m working through Basic Data Structures: Iterate through all an array’s items using for loops.

I ran into the error “newArr.push is not a function”.

In searching google several other people had encountered this problem with .push when the type of the array has somehow been switched to object, or when they tried to run it on something that was not an array in the first place.

So I console.logged the type of newArr right before I tried .push - and sure enough it showed up as an object.

To try to figure out where this had happened I moved the typeOf check up line by line. Eventually I got all the way to the top of the section I’m supposed to edit, (right after newArr is initialized empty, and before any of my own code starts).

It still returns as an object. Why is this?

In looking at others here on the forum it seems they are able to use array only methods on newArr.

I am using
Chrome Version 70.0.3538.110 (Official Build) (64-bit)
Windows 10 home version 1803 build 17134.417

if you want any help, it would be really useful if you posted the code you are talking about. If you use the “Ask for Help” button that happens automatically

remeber though that an array is a type of object

1 Like

Hi @auntjeanne1775,

In javaScript, typeOf(Array) is always an object. You can refer it here:

If you want to check the type of array, you can use Array.isArray() method, which returns true if the passed value is an Array. You can refer it here:

1 Like

To piggy back on to what others have said typeof(newArr) will return "object". Another way of making sure it’s actually an array before performing any methods you can do newArr instanceof Array and it will return a boolean. instanceof will check newArr’s prototype chain for the presence of Array.prototype which would indicate that newArr inherits from the Array class. Read up more here