JavaScript条件和循环以及异常处理

                    JavaScript条件和循环以及异常处理

                                        作者:尹正杰

版权声明:原创作品,谢绝转载!否则将追究法律责任。

 

一.布尔类型(Boolean)

布尔类型仅包含真假,与Python不同的是其首字母小写。

  • ==      比较值相等
  • !=       不等于
  • ===   比较值和类型相等
  • !===  不等于
  • ||        或
  • &&      且

 

二. 条件语句

  JavaScript中支持两个中条件语句,即if 和 switch。

1.if语句

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>尹正杰的if条件语句</title>
 6 </head>
 7 <body>
 8 <script>
 9     time =20;
10     if (time<10)
11     {
12         document.write("<b>早上好</b>");
13     }
14     else if (time>=10 && time<16)
15     {
16         document.write("<b>今天好</b>");
17     }
18     else
19     {
20         document.write("<b>晚上好!</b>");
21     }
22 </script>
23 </body>
24 </html>

2.switch语句

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>尹正杰的switch语句</title>
 6 </head>
 7 <body>
 8 <h1 id = "100"></h1>
 9 <script>
10     var d = new Date().getDay();
11 switch (d )
12 {
13   case 0:x="今天是星期日";
14   break;
15   case 1:x="今天是星期一";
16   break;
17   case 2:x="今天是星期二";
18   break;
19   case 3:x="今天是星期三";
20   break;
21   case 4:x="今天是星期四";
22   break;
23   case 5:x="今天是星期五";
24   break;
25   case 6:x="今天是星期六";
26   break;
27 }
28     document.getElementById("100").innerHTML = x;
29 </script>
30 </body>
31 </html>

 

三.循环语句

1.取索引

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>尹正杰的循环语句一</title>
 6 </head>
 7 <body>
 8     <script>
 9         var NameList = ["yinzhengjie","linux","mysql"];
10         for (var index in NameList){
11 //            alert(index)
12             console.log(index)    //此处我们只能拿到NameList所对应的索引。
13         }
14     </script>
15 </body>
16 </html>

2.取值循环语句

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>尹正杰的循环语句二</title>
 6 </head>
 7 <body>
 8     <script>
 9         var NameList = ["yinzhengjie","linux","mysql"];
10         for(var index=0;index<NameList.length;index++){
11             console.log(NameList[index]);     //我们这里是将下标取出来之后,再去找下标对应的值。
12         }
13     </script>
14 </body>
15 </html>

3.while循环语句

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>尹正杰的循环语句三</title>
 6 </head>
 7 <body>
 8     <script>
 9         var count =1;
10         while (count<10){
11              console.log(count);    //此时我们就可以在浏览器console窗口打印数字1-9啦。
12             count ++;
13         }
14     </script>
15 </body>
16 </html>

 

四.异常处理

 1 try {
 2     //这段代码从上往下运行,其中任何一个语句抛出异常该代码块就结束运行
 3 }
 4 catch (e) {
 5     // 如果try代码块中抛出了异常,catch代码块中的代码就会被执行。
 6     //e是一个局部变量,用来指向Error对象或者其他抛出的对象
 7 }
 8 finally {
 9      //无论try中代码是否有异常抛出(甚至是try代码块中有return语句),finally代码块中始终会被执行。
10 }

  具体案例可以参考:http://www.runoob.com/js/js-errors.html

 

 

 

 

学如逆水行舟,不进则退,推荐几个不错的js学习网址:

  1>.http://www.runoob.com/js/js-tutorial.html(我推荐使用这个,因为比较火的语言这个网站都有!)

  2>.http://www.w3school.com.cn/b.asp

  如果你学习迷茫了,自学觉得累了,可以和我们一起来探讨只是点,大家共同激励进步,欢迎加入高级运维工程师之路。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值