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?
Asked
Active
Viewed 3,370 times
2
-
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 – Elmer 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. – But those new buttons though.. Jan 30 '17 at 00:39
-
1Per 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 Answers
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
-
i agree - doing variable assignments in the console usually spits out "undefined". try logging `test` after declaring it and see what you get – But those new buttons though.. Jan 30 '17 at 00:40
-
1
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.

Soviut
- 88,194
- 49
- 192
- 260