本文介绍了“var variable”返回undefined?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我运行var variable = true;时在Chrome控制台中我得到未定义返回:
When I run "var variable = true;" in chrome console I get "undefined" returned:
> var variable = true;
undefined
但是当我没有var运行时它返回true:
But when I run without "var" it returns true:
> variable = true;
true
为什么用var返回undefined?
Why is it returning "undefined" with "var"?
令人困惑的是因为我预计它会返回true。
It's confusing cause I expected it would return true.
推荐答案
第一个是声明,而第二个是表达。虽然不完全相同,但它类似于C的规则:
The first is a statement, while the second is an expression. While not quite the same, it is similar to C's rules:
// A statement that has no value.
int x = 5;
// An expression...
x = 10;
// ...that can be passed around.
printf("%d\n", x = 15);
这篇关于“var variable”返回undefined?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!