2017年8月15日 星期二

[javascript] 判斷變數 null、undefined及空值

https://blog.camel2243.com/2016/03/02/javascript-%E5%88%A4%E6%96%B7%E8%AE%8A%E6%95%B8-null%E3%80%81undefined%E5%8F%8A%E7%A9%BA%E5%80%BC/

  1. 判斷變數為 null 或 undefined 或 “”
    if(!str)
    {
          var str;
          console.log("str is null or undefined or '' ");
    }
    
  2. 判斷變數是否存在
    if(typeof str === "undefined")
          console.log("str is not exist");
    
    PS. === 可以辨別型態, == 則會轉型單純比值

沒有留言:

張貼留言