码迷,mamicode.com
首页 > Web开发 > 详细

if判断【js】

时间:2016-10-23 21:08:29      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:span   class   自动   转换   控制   条件   else   htm   pretty   

if判断语句
格式:
if(判断条件){
符合条件的代码;
}else{
不符合条件的代码;
}
if语句特殊之处:在javascript中的if语句条件不但可以写布尔表达式,还可以写任何的数据。
判断的条件会发生自动类型转换
number:如果非0为true,0为false
string:如果非null或非空为true,否则为false
undefined:false
NaN:    false
对象类型:非null为true,否则为false。
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>控制流程语句</title>
  6. <script type="text/javascript">
  7. /*
  8. if判断语句
  9. 格式:
  10. if(判断条件){
  11. 符合条件的代码;
  12. }else{
  13. 不符合条件的代码;
  14. }
  15. if语句特殊之处:在javascript中的if语句条件不但可以写布尔表达式,还可以写任何的数据。
  16. 判断的条件会发生自动类型转换
  17. number:如果非0为true,0为false
  18. string:如果非null或非空为true,否则为false
  19. undefined:false
  20. NaN: false
  21. 对象类型:非null为true,否则为false。
  22. */
  23. var age = 19;
  24. if(age >= 18){
  25. document.write("成年");
  26. }else{
  27. document.write("未成年");
  28. }
  29. document.write("<br/>");
  30. //if语句特殊之处
  31. var condition = NaN;
  32. condition = "";
  33. condition = null;
  34. condition = 9;
  35. condition = 0;
  36. condition = -22;
  37. condition;
  38. if(condition){
  39. document.write("满足条件");
  40. }else{
  41. document.write("不满足条件");
  42. }
  43. </script>
  44. </head>
  45. <body>
  46. </body>
  47. </html>





if判断【js】

标签:span   class   自动   转换   控制   条件   else   htm   pretty   

原文地址:http://www.cnblogs.com/itMiracle/p/5990745.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!