1. 程式人生 > >JavaScript學習 - 基礎(四) - 控制語句/異常處理

JavaScript學習 - 基礎(四) - 控制語句/異常處理

控制語句

  if-else 語句

<script>
    //控制語句
        //if-else格式:
    var x = 1
        if(x==1){
            console.log("this is if")
        }else if(x>1){
            console.log("this is else if")
        }else {
            console.log("this else")
        }
</script>

 

  switch語句

<script>
    //switch語句
    var x = 8;
    switch (x){
        case 1:alert("this 1");break;
        case 2:alert("this 2");break;
        case 3:alert("this 3");break;
        case 4:alert("this 4");break;
        case 5:alert("this 5");break;
        case 6:alert("this 6");break;
        
case 7:alert("this 7");break; default:alert("nothing") } </script>

 

  for迴圈語句

<script>
    // for迴圈:for while
    // 基本格式:
    for(初始化;條件;增量){語句}
    for(i=1;i<100;i++){
        document.write(i)
        document.write('<br>')
    }

    var x = [1,2,3,4,5,6]
    
for(i=0;i<x.length;i++){ document.write(x[i]) document.write('<br>') } //不建議使用 var x = ['a','b','c','d'] for(i in x){ document.write('this i,輸出:'+i); document.write('<br>'); document.write('this x[i],輸出:'+x[i]); document.write('<br>'); document.write('----------------------'); document.write('<br>'); } // this i,輸出:0 // this x[i],輸出:a // ---------------------- // this i,輸出:1 // this x[i],輸出:b // ---------------------- // this i,輸出:2 // this x[i],輸出:c // ---------------------- // this i,輸出:3 // this x[i],輸出:d // ----------------------
  
  <body>
   <p>A</p>
  <p>B</p>
  <p>C</p>
  <p>D</p>
  <p>E</p>
  <p>F</p>
  <p>G</p>
  </body>
  //不建議使用的原因
    var x2 = document.getElementsByTagName('p')
    for(i in x2) {
        document.write('this i,輸出:' + i);
        document.write('<br>');
        document.write('this x[i],輸出:' + x2[i]);
        document.write('<br>');
        document.write('----------------------');
        document.write('<br>');
    }
    // this i,輸出:0
    // this x[i],輸出:[object HTMLParagraphElement]
    // ----------------------
    // this i,輸出:1
    // this x[i],輸出:[object HTMLParagraphElement]
    // ----------------------
    // this i,輸出:2
    // this x[i],輸出:[object HTMLParagraphElement]
    // ----------------------
    // this i,輸出:3
    // this x[i],輸出:[object HTMLParagraphElement]
    // ----------------------
    // this i,輸出:4
    // this x[i],輸出:[object HTMLParagraphElement]
    // ----------------------
    // this i,輸出:5
    // this x[i],輸出:[object HTMLParagraphElement]
    // ----------------------
    // this i,輸出:6
    // this x[i],輸出:[object HTMLParagraphElement]
    // ----------------------
    // this i,輸出:length
    // this x[i],輸出:7
    // ----------------------
    // this i,輸出:item
    // this x[i],輸出:function item() { [native code] }
    // ----------------------
    // this i,輸出:namedItem
    // this x[i],輸出:function namedItem() { [native code] }
</script>

 

  while迴圈語句

<script>
    //while 迴圈
    var i=0;j=0;
    while(i<201){
        j +=i
        i++;
    }
    document.write(j)
</script>

 

異常處理

<script>
    //異常
    //try{主程式}catch(異常){執行}finally{無論是否異常都執行}
    //主動丟擲異常 throw Error("xxxx")
    try{
        throw Error("報錯了!!!")
        document.write("OK")
    }
    catch (e) {
        document.write("異常")
    }
    finally {
        document.write("OK and OK")
    }
</script>