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

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

                                        做者:尹正杰
mysql

版权声明:原创做品,谢绝转载!不然将追究法律责任。linux

 

一.布尔类型(Boolean)sql

布尔类型仅包含真假,与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语句spa

 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>

 

三.循环语句code

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

  若是你学习迷茫了,自学以为累了,能够和咱们一块儿来探讨只是点,你们共同激励进步,欢迎加入高级运维工程师之路。

相关文章
相关标签/搜索