2

Hey pretty simple task I am trying to do.... I'm trying to take the text in my textbox and assign it to a var. When I run without the var I am getting text but if I assign it to a var I get undefined. Could someone explain this to me as it is very confusing to me?

enter image description here

Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
Seamy
  • 287
  • 4
  • 14

2 Answers2

5

TL;DR: It does not.

You can see content of your variable test, il will output the same thing as before. In fact it is the variable assignement that returns the undefined you see here.

For instance:

var test = 'Hello' // => undefined
test // => 'Hello'

Another case is printing your variable with console.log. If you do so, the return value will be undefined but the output will be your variable content (Hello here).

console.log(test) // return: undefined / print: Hello
Ulysse BN
  • 10,116
  • 7
  • 54
  • 82
0

What's returning undefined is the statement itself that you entered into the console, NOT the value of var text.

To see that console.log(text) or simply type text in the console.

enter image description here

Soviut
  • 88,194
  • 49
  • 192
  • 260