为什么在开发工具中检查JS中的赋值变量时未定义?

时间:2018-08-04 02:53:48

标签: javascript

为什么在开发工具中检查JS中未分配的变量?

例如,

var x = 5;

在devtools中导致未定义。

3 个答案:

答案 0 :(得分:2)

控制台不会评估x的值,但会评估表达式本身,表达式始终在javascript中未定义。

答案 1 :(得分:1)

示例1 =>

var x = 55; // undefined    

它声明变量x并将其赋值为undefined。这就是我们从控制台上获得的反馈值。

然后,它最终将值55分配给x。此时控制台已经返回了一个值,因此当我们一次声明并分配一个变量时,我们不会看到值55作为反馈。

另一方面,如果稍后将变量x重新分配给其他值,我们将获得新值作为反馈,而不是未定义:

示例2 =>

x = 57; //57

答案 2 :(得分:0)

We are declaring a variable but of which type it does not define (like string, int, or boolean) that's why it displays undefined as a first statement. after it assigns a value to a variable and decides the type of variable in Javascript.

like var a=10;  // undefined as first time when var is created.

typeof(a) // "number" in second statement

    -- IN addition for the function in Javascript ---------------    



    (function (){return("OK")})()
    (function (){})()

    Undefined is about the return value of a function call.
    You only see something useful when a function returns value.
    If nothing is returned then you see undefined.