1

I am learning JavaScript on Codecademy. I am finding so many outputs like below when I use while loop.

for (var i=0; i<5; i++){
        console.log("Dhanu identify problems and solve them");
}

var loop = 0;
while(loop<5){
    console.log("Do some more");
    loop++;
}

I am getting an unwanted output in the last line as 4.

enter image description here

Regis Portalez
  • 4,675
  • 1
  • 29
  • 41
Dhanu AT
  • 39
  • 4
  • 4
    Possible duplicate of [Javascript while loop return value](http://stackoverflow.com/questions/35454291/javascript-while-loop-return-value) – James Thorpe May 16 '16 at 12:53
  • 1
    @Reddy Try running it in a browser console, the 4 still shows up. I guess it's the result of the last assignment which is returned and printed – chrki May 16 '16 at 12:53
  • This may be an issue with the Codecademy output. If there are no other lines of code, this would definitely not print a "4" (as you can see in this fiddle - https://jsfiddle.net/w14r4psr/). I would recommend asking the question on the Codecademy forum. – WonderGrub May 16 '16 at 12:55
  • @chrki Right.. when I paste it in console it does show the 4.. But when I put this is fiddle its different.. – Rajshekar Reddy May 16 '16 at 12:57

3 Answers3

2

The output you get is simply console.log which logs the last known value of the 'loop' variable: (here in firefox):

enter image description here

It's absolutely not related to your code and you don't have to worry about that.

As stated in another answer, debug console use to log the result of the last line (probably because they are meant to debug).

For example the statement "i = 1" evaluates to "1":

enter image description here

while the statement "var i = 1;" evaluates to "undefined", hence logging "undefined"

you can observe the same behavior by invoking eval on those statements:

enter image description here

Regis Portalez
  • 4,675
  • 1
  • 29
  • 41
  • 2
    `The output you get is simply console.log which logs the last known value of the 'loop' variable:` But why? – Rajshekar Reddy May 16 '16 at 12:59
  • because of the internal implementation of debug panel (which I don't really know in depth), which is probably based somehow on eval (see my edited answer) – Regis Portalez May 16 '16 at 13:09
0

You're seeing that because the last statement in your while loop is loop++, which "returns" the current value of loop. This is just a side effect of running code in a console like this. Consoles are basically read-execute-print-loops (REPL), and so they need something to print. If you ran this as a script in a webpage you would not see 4

grdaneault
  • 830
  • 1
  • 11
  • 21
  • Thanks. I knew it is not something that I should worry about. But still needed to know what is going on. – Dhanu AT May 16 '16 at 13:00
0

when you write any statement in developer console then last returned value will be printed...

for example

var a;

it prints undefined on console.

a = 10;

it prints 10 on console.

a = 10; a = a + 5;

it prints 15 on console.

simply when we assign any values or increment or decrement numbers then it returns value after performing that operation...

so, in the above or below code

var loop = 0;
while(loop<5){
    console.log("Do some more");
    loop++;
}

finally loop++ is 4.

ankit
  • 125
  • 1
  • 3