为什么Chrome控制台在这里没有返回undefined?

时间:2019-08-10 20:56:27

标签: javascript asynchronous

示例1:

var Hoist;
console.log(Hoist);
Hoist = "Variable is hoisted";

示例2:

console.log(Hoist);
var Hoist = "Variable is hoisted";

我期望由于吊装,两个示例的console.log都将打印未定义。 但是,以下是输出- enter image description here enter image description here

根据我对示例1的理解,

执行葫芦=“悬挂变量”;在编译步骤中被跳过 在解释步骤中,console.log()无法使用Hoist的值;因为分配尚未发生。

console.log()如何获取示例编号中的值。 1?

1 个答案:

答案 0 :(得分:5)

您误将return语句用作控制台语句。
在您提供的图像中,右箭头显示您的输入,无箭头的行是console.log()的结果,左箭头是返回值。

您的两个示例均按预期记录undefined,但是,除此之外,第一个示例的返回值为undefined,第二个示例具有字符串。

相关问题