Now we are going to talk about arrays. But what is an array? An array is a collection of data. It could be a primitive data type, or reference data type. So for example, we can have an array of foods, foods equals to just add square brackets. And then inside these you add the data that you want to collect inside the inside this array.
So for example, I have apple, orange, and fish. Right? So we have three elements in this array. And as we learned before, everything in JavaScript is an object, right? And arrays are also objects. Therefore, we can create arrays another way using the new keyword.
So how How are we going to do so what we are going to do is just type for var n foods. And instead of using the square bracket mutations, what I'm going to do is I'm going to type equals a new, and then capital A, and then array, and parentheses, and semicolon. So now this is an array. And as you can see, arrays are also objects. By the first version of this array is that it has three elements. The second version is an empty array.
So how are we going to add elements to this array to the secondary, or even the first array? It's so simple, if you already have elements and you want to add elements, adding elements. What you would do is you you use a function called push which is going to add elements to the tail of the Alright, so for example, if I comment this out and use the first array only the first version and the US foods dot push, and I add the element that they want to add to this array or push to this array, for example, it's coffee. Right. And then I use the console, console, that log. And then I just print this array, which is called foods.
And Save and Run, I'm going to get apple, orange fish and coffee because bush will add that element inside the array at the end of this array. And if we use the push on the second version, if I comment this out, and use it on the second version, what I'm going to get is that I'm going to get only coffee because the second version is empty array. So this is how you can add elements to an array if the array is already created.