JS判断元素是否被定义

2018-04-04  本文已影响17人  丶灰太狼他叔

如想判断a标签是否被定义时通常会这样写:

 if (a) {
       console.log('liuhuan')l;
   }

这样是会抛出异常Unexpected identifier的,即意外的标识符。所以通常用typeof 是否为undefined来判断:

if (typeof a == "undefined") {
       console.log('liuhuan');
   }

值得注意的是:typeof 返回的为一个字符串!

上一篇下一篇

猜你喜欢

热点阅读