Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Array.prototype return an empty array?

It might be a stupid question, but why do I get an empty array when I console out Array.prototype ?

I was expecting the push and pop methods.

like image 968
Moon Avatar asked Aug 31 '12 22:08

Moon


People also ask

Why is empty array true?

Because Array is type of object , the fact that an empty Array is conversed to true is correct.

What does an empty array return JavaScript?

Now we can check if the array is empty by using .length . This will return 0, as there are 0 items in the array.

Why empty array is false?

arrays are objects, objects are truthy. just ask for array. length, if not zero, it will be truthy. when you explicitly convert to Boolean, the array turns into an empty string first, then the empty string turns into false.

Why empty array is true in JavaScript?

Values not on the list of falsy values in JavaScript are called truthy values and include the empty array [] or the empty object {} . This means almost everything evaluates to true in JavaScript — any object and almost all primitive values, everything but the falsy values.


1 Answers

Most of the built-in methods of prototypes are not enumerble, so they won't show up if you use for..in (which you shouldn't on arrays, but this is just an example). Since they not enumerable, they won't show up if you "console them out".

like image 158
Niet the Dark Absol Avatar answered Oct 13 '22 23:10

Niet the Dark Absol