如何检查JavaScript变量是否未定义?

2022-08-30 00:41:55

我尝试过的东西似乎不起作用:

if(lastName != "undefined")
if(lastName != undefined)
if(undefined != lastName)

答案 1
var lastname = "Hi";

if(typeof lastname !== "undefined")
{
  alert("Hi. Variable is defined.");
} 

答案 2