Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在寻找一种快速检查布尔变量而不检查它是否存在的方法,例如:
if(variable){ alert(variable); }
但在这里我可能会得到“ReferenceError:变量未定义”
用于typeof检查变量是否存在。
typeof
if((typeof variable !== "undefined") && variable) { ... }
尝试
if(typeof variable != 'undefined'){ alert(variable); }