I get that the #1 var is an array
then we have a normal variable with number data type = 0;
When it comes to the while loop what I don’t get is what happens to ourArray.push(i);
How is it getting the variable i ?
And what is exactly doing the push()
var ourArray = [];
var i = 0;
while(i < 5) {
ourArray.push(i);
i++;
}
Also when you add a variable to another like this ourArray.push(i);
What it is called technically in JS language? I can’t google because I don’t know what to call it.
In this case, i is a number which starts at 0. During the while loop, if i < 5 is true, then we .push the current value of i to ourArray and increment i by one (i++).
.push is a built-in array method that takes a single argument and sticks that value on the end of the array. As for the terminology, this could be called an “Array prototype method”. The process of modifying the array like this would be “mutation”.
So in plain English it would be something like this:
var ourArray = [];
var i = 0;
while(0 < 5 ) {
ourArray.push(i); // take the array and add the value of i
i++; then each time increase by 1 until it becomes 0, 1, 2, 3, 4, 5 then 5<5 = false?
}
If you really want to understand this well, and it sounds like you do, what you need to understand here are objects, methods and prototypes in Javascript.
To answwer the question, an array is a data structure, stored in the memory of your computer.
The push() is being done by the Javascript “runtime”, in otherwords code that is running inside the browser.
The push() belongs to the object ourArray. What happens is javascript looks at ourArray and sees if it has a property called push. If it does it will use that. In this case it doesn’t (you didn’t define it) so it looks at the prototype of ourArray.
This prototype is the array build into Javascript. And this object does have a push() method. Inside that push() method code will be run by the Javascript “runtime” to add that value to the array behind the scenes. It will do this by allocating more of the computer’s memory (if needed) and then writing the value to memory.
You’ve made a small but very very important modification here. This while loop would never stop running. Because while(0 < 5) is always true. The while block would just keep counting up and pushing items to the array, which would get longer and longer and longer… and i would get bigger and bigger and bigger… until the browser crashes.
Maybe a topic to google that is closer to your level of learning is “variable scope”. It’ll explain why the while loop (in its original version) knows the value of the variable named i, and why the while loop is able to modify that value (add 1 to it), and why it can use that value to test on each loop if the condition is still true and if it should stop running or continuing.
push just means - take the array on the left side of the dot, and add an item to it at the end. The variable name ourArray still points to the same array, but after pushing, it is one item longer.
No problem. It is advanced, and JavaScript isn’t the easiest language. It’s ok not to get it now, and you probably don’t need it to “get the job done”. It’s handy to have the keywords to search for later.
Understanding roughly how stuff is stored in memory might make understanding what is going on a lot easier, and you will understand why things are like they are a bit, hopefully. Especially with references vs. values, stuff like that.
yes, but only the first time, if it was always that would be an infinite loop and break everything - instead, the second time that condition is checked, i is 1, the third i is 3, and so on
Thanks. I’ll note them for later use.
The easiest way to understand programming or anything in life is asking questions like "how? " and “why ?” is it working that way.