What is it [] used for?

Hello everyone! This is my second post with the same questions. I got that it is an empty array, but what is it used for?

Example: This challenge from JS

var ourArray = []; HERE! I do not get why they put this! help please! :/
var i = 0;
while(i < 5) {
  ourArray.push(i);
  i++;
}

is this merely to create an empty space where the code is going to fill with information?

Yes! In order to be able to store the i's, you need to create some place for it. Since it’s going to be a collection of i's, an array would be the best fit to store them.
Does this make sense?

2 Likes

if it would not be declared as array, you probably would not be able to use ‘ourArray.push()’ method on it. since push() is method only useable on array type. try deleting empty brackets, im ~90% sure you would get error " push() is not a function" …i think :smiley:

1 Like

Yes, it makes sense. Thanks a lot!

no, javascript is not c++ , it does not need to have prealocated space to create any variables.

1 Like

It makes sense, as a newbie, I wanted to confirm this. Thanks man!

1 Like