簡體   English   中英

為什么在開發工具中檢查JS中的賦值變量時未定義?

[英]Why an assigned variable in JS is undefined when inspected in dev tools?

為什么在開發工具中檢查JS中的賦值變量時未定義?

例如,

var x = 5;

在devtools中導致未定義。

控制台不會評估x的值,但會評估表達式本身,表達式始終在javascript中未定義。

示例1 =>

var x = 55; // undefined    

它聲明變量x並為其分配undefined值。 這就是我們在控制台上獲得的反饋值。

然后,它最終將值55分配給x。 此時控制台已經返回了一個值,因此當我們一次聲明並分配一個變量時,我們不會看到值55作為反饋。

另一方面,如果稍后將變量x重新分配給其他值,我們將獲得新值作為反饋,而不是未定義:

示例2 =>

x = 57; //57
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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM