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
  • 8,503
  • 5
  • 45
  • 74
Seamy
  • 277
  • 3
  • 12
  • use .val() instead if you are dealing with inputs – Bryan Dellinger Jan 30 '17 at 00:31
  • That just returns undefined aswell... I was trying to use that originally – Seamy Jan 30 '17 at 00:35
  • Maybe this answer can help you: http://stackoverflow.com/questions/22844840/why-does-javascript-variable-declaration-at-console-results-in-undefined-being – ELM Jan 30 '17 at 00:36
  • it's always better to add actual code to your question and not a screenshot of code. you can add your markup and js to a code snippet to illustrate the problem. – Eaten by a Grue Jan 30 '17 at 00:39
  • 1
    Per spec, `var` statements return _undefined_ http://es5.github.io/#x12.2 . The identifier `test` will still be initialised and set. Type `test;` and you will see its contents – Paul S. Jan 30 '17 at 00:42
  • But if I try console.log(test) I still get undefined? – Seamy Jan 30 '17 at 00:43

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
  • 8,503
  • 5
  • 45
  • 74
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
  • 83,904
  • 44
  • 175
  • 239