Search code examples
javascriptfirebug

Firebug's console.log prints unexpected value using javaScript


I am new to javascript and was playing with simple code but when i wrote this below specified code in firebug, i got the unexpected value. Here is the code

    var a=5;
    while(a<10000){
       if(a%1000==0)
         console.log(a);
       a++;
    }

And this was the output in console

    var a=5; while(a<10000){   if(a%1000==0)     console.log(a);   a++; }
    1000
    2000
    3000
    4000
    5000
    6000
    7000
    8000
    9000
    9999

At the end it gave me the "9999", which it should not have printed according to the logic.
While, i replaced the console.log by alert, i received the exact output.
I feel it is simple, but i have never used firebug and i was a little curious to know about why this happened?


Solution

  • Your problem is that firebug does always print the return value of the last statement. If you change your statement to this:

    var a=5; while(a<10000){   if(a%1000==0)     console.log(a);   a++; };null;
    

    you will get null instead of 9999. The 9999 is the return value of the last a++. This is only if you enter you code in the console, and so you can just enter a variable and its printed.