js判断变量是否未定义的代码

  例如:

  if(!myVar01)alert("发生错误");

  // 该代码直接发生异常,因为变量myVar01没有申明 if("undefined" == typeof myVar01)alert("发生错误");

  // 这样写才不至于发生异常

  而: var myVar01; if(undefined == myVar01)alert("发生错误");

  // 该代码会正确运行 if("undefined" == typeof myVar01)alert("发生错误");

  // 该代码同样会正确运行

  结论:我们采用下面的方式来保证万无一失 if("undefined" == typeof myVar01)alert("发生错误");

  // 该代码同样会正确运行

  当然判断数据的有效性远远不只这些,还有对null的判断,数字是否大道越界.

  实际应用:

  downlm我们不定义

  if("undefined" != typeof downlm){

  if(downlm=="soft"){

  document.write('成功');

  }

  }