为什么这个 js 条件不起作用 - if ( xxx === undefined )

Why this js condition does not work - if ( xxx === undefined )

我虽然这个签名应该有效,但它抛出一个错误并停止继续脚本。

if ( xxx === undefined ) alert('aaaaaaaaaaaaaaa');

为什么它不起作用。它应该工作。我发现很多答案都提到了这个签名来测试变量是否未定义。

如果你想检查一个变量是否存在,你需要使用 typeof

if ( typeof xxx === 'undefined' ) alert('aaaaaaaaaaaaaaa');

如果你不这样做,你会得到一个错误:

xxx is not defined