#5, Why does this work? purpose of count++?


var count = 0;

var loop = function(){
    while(count < 3){
        console.log("I'm looping!");


So i've figured out that this is the answer, but I am not sure why this works.
Without adding the line count++, it loops forever and crashes. But why does it stop it from looping forever?

I thought that ++ was just the same as + 1.


You can replace count++; with count = count + 1;.


I understand that they are the same thing, but why does that stop the function instead of it making it go on forever? There is the condition (count < 3) indicating to run as long as its less than 3, so wouldn't it stop even without count++?

Also, why are the () left blank for function and the last line "loop();"?


No, because without count++ the value of count wouldn't change each time, meaning it's always < 3 meaning it will run forever.


Because the loop function does not expect any parameter.


Thanks much @jibblyj @eveat. Got it.


This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.