在JavaScript REPL环境中将表达式赋值给变量并运行时会产生不同的结果。

huangapple go评论57阅读模式
英文:

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]"

huangapple
  • 本文由 发表于 2023年7月18日 00:15:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定