简体   繁体   English

为什么在分配变量时控制台打印未定义?

[英]Why does the console print undefined when I assign a variable?

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. 嘿,我正在尝试做的非常简单的任务。...我正在尝试将文本放在文本框中,并将其分配给var。 When I run without the var I am getting text but if I assign it to a var I get undefined. 当我不使用var运行时,我正在获取文本,但是如果将其分配给var,则无法定义。 Could someone explain this to me as it is very confusing to me? 有人可以给我解释一下,因为这让我很困惑?

在此处输入图片说明

TL;DR : It does not. TL; DR :没有。

You can see content of your variable test , il will output the same thing as before. 您可以看到变量test内容,il将输出与以前相同的东西。 In fact it is the variable assignement that returns the undefined you see here. 实际上,变量赋值返回您在此处看到的undefined

For instance: 例如:

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

Another case is printing your variable with console.log . 另一种情况是使用console.log打印变量。 If you do so, the return value will be undefined but the output will be your variable content ( Hello here). 如果这样做,则返回值将是undefined但输出将是您的变量内容(此处为Hello )。

console.log(test) // return: undefined / print: Hello

What's returning undefined is the statement itself that you entered into the console, NOT the value of var text . 返回undefined是您在控制台中输入的语句本身,而不是var text的值。

To see that console.log(text) or simply type text in the console. 要查看console.log(text)或仅在控制台中键入text

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这段 JavaScript 代码会在控制台上打印“undefined”? - Why does this JavaScript code print “undefined” on the console? 为什么 Chrome 和 FireFox 控制台打印“未定义”? - Why does Chrome & FireFox console print 'undefined'? 为什么这个打印未定义? 当我使用 ${name} 以外的变量时,它会起作用 - Why does this print undefined? When I use a variable other than ${name} it works 为什么当我 console.log() 时它说未定义? - Why does it say undefined when i console.log()? 当我可以打印未定义的变量时,为什么会出现“无法读取未定义错误的属性”? - Why Am I Getting “Cannot read property of undefined error” When I Can Print the Undefined Variable? “i”变量在控制台中未定义 - “i” variable is undefined in console 为什么不能在控制台中将检索到的文档分配给变量 - Why can't I assign a retrieved document to variable in console 为什么即使我在前两行定义了变量,JavaScript控制台仍说我的变量未定义? - Why is the JavaScript console saying my variable is undefined, even when I defined it two lines before? 为什么控制台上的 JavaScript 变量声明会导致打印“未定义”? - Why does JavaScript variable declaration at console results in “undefined” being printed? 当我输入json对象时,firebug控制台为什么总是说“ undefined”? - Why does the firebug console keep saying “undefined” when I enter a json object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM