英文:
Different Results when assigning an expression to a variable and running it in the JavaScript REPL environment
问题
我目前正在尝试深入理解JavaScript类型转换。在使用node REPL环境时,我遇到了语句[]+{}
和let x = []+{}
。执行第二个语句后,变量x的值为undefined
,尽管我预期这两个语句的表达式都应该评估为'[object Object]'
。
是什么导致了这种差异?
英文:
I am currently trying to develop a deeper understanding of JavaScript type coercion. While playing around with the node REPL environment, I encountered the statements []+{}
and let x = []+{}.
After executing the second statement, the variable x holds the value undefined
, although I expected that the expression evaluates to '[object Object]'
in both statements.
What causes this difference?
答案1
得分: 1
这是因为 let
语句返回 undefined
,而不是变量值。要检索变量值,您必须键入变量名称。
let x = [] + {};
// undefined
x;
// " [object Object] "
英文:
That's because let
statement returns undefined
, not variable value. To retrieve variable value, you have to type the variable name.
let x = [] + {};
// undefined
x;
// "[object Object]"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论