簡體   English   中英

以下代碼行的 output 是什么? 變量 x = null; 變量 z = “sp”; 如果 (x==true){ z+=1; } 控制台.log(z); *

[英]What is the output for the following lines of code? var x = null; var z = “sp”; if (x==true){ z+=1; } console.log(z); *

以下代碼行的 output 是什么? 變量 x = null; var z = "sp"; 如果 (x==true){ z+=1; } 控制台.log(z); *

Null是 Javascript 中的假值。 因此,條件不會變為真。 您將獲得的 output 肯定是“sp”。

 var x = null; var z = "sp"; if (x==true){ z+=1; } console.log(z);

0、0n、null、undefined、false、NaN和空字符串“”在JS中都是假值。 如果您嘗試使用任何虛假值運行上述代碼,則您的條件將評估為 false,並且括號之間的代碼將永遠不會運行。

 var x; var z; x = null; z = "sp"; if (x==true){ z+=1; } console.log(z); x = 0; z = "sp"; if (x==true){ z+=1; } console.log(z); x = ""; z = "sp"; if (x==true){ z+=1; } console.log(z); x = undefined; z = "sp"; if (x==true){ z+=1; } console.log(z); x = false; z = "sp"; if (x==true){ z+=1; } console.log(z); x = NaN; z = "sp"; if (x==true){ z+=1; } console.log(z);

暫無
暫無

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

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