0

While I am running the following piece of code in Chrome's developer tools javascript console,

var solution = 1;
for(var i = 1; i <= 12; ++i)
    solution *= i;
console.log(solution.toString());

I get the following output:

479001600
undefined

When I remove the last line, that is console.log(solution.toString());, I get just 479001600. What happens?

j08691
  • 204,283
  • 31
  • 260
  • 272
Ali Shakiba
  • 1,255
  • 2
  • 15
  • 29

2 Answers2

5

The undefined is simply the console's way of telling you that the statement:

console.log(solution.toString());

doesn't, itself, return a value (it outputs a value, but doesn't return one). It's not something to worry about when your actual code executes because it's a specific behavior to the developer's console.

As another example, if you type: 5 + 6 into the console, it will report 11 on the next console line, because the console always wants to report any value returned from the statement it just executed.

Scott Marcus
  • 64,069
  • 6
  • 49
  • 71
1

The console in google chrome will always return the value of the last executed statement.

Since your last statement in the console was

console.log()

one which did not return any value it showed 'undefined';

Try

var a=[];a.push(5);

It will show 1 on the next line as the push method returns the length of the newly formed array

Joey Pinto
  • 1,735
  • 1
  • 18
  • 34