Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
1
rated 0 times [  6] [ 5]  / answers: 1 / hits: 127718  / 10 Years ago, sat, june 21, 2014, 12:00:00
console.log(hi) gives 
undefined
hi

console.log(1+1) gives
undefined
2


Whether it's a string or integer calculation, I get undefined then the correct answer.



Why do I get the undefined message? Is there a good way to avoid it?


More From » console.log

 Answers
16

The console will print the result of evaluating an expression. The result of evaluating console.log() is undefined since console.log does not explicitly return something. It has the side effect of printing to the console.



You can observe the same behaviour with many expressions:



> var x = 1;
undefined;


A variable declaration does not produce a value so again undefined is printed to the console.



As a counter-example, expressions containing mathematical operators do produce a value which is printed to the console instead of undefined:



> 2 + 2;
4

[#70487] Thursday, June 19, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
davonte

Total Points: 581
Total Questions: 101
Total Answers: 113

Location: Sudan
Member since Tue, Aug 3, 2021
3 Years ago
;