(四)JavaScript之[break和continue]与[typeof、null、undefined]

(四)JavaScript之[break和continue]与[typeof、null、undefined]

7】、break和continue

 1 /**
 2 * JavaScript 的break和continue语句
 3 * break 跳出switch()语句
 4 * break 用于跳出循环
 5 * continue 用于跳过循环中的一个迭代*/
 6 
 7 // break 跳出循环
 8 for(var i = 0;i < 10;i++){
 9     if(i == 3){
10         break;
11     }
12     console.log('The number is: ' + i);
13 }
14 
15 // continue 跳过循环
16 for(var i = 0;i < 10;i++){
17     if(i == 3){
18         continue;
19     }
20     console.log('The number is: ' + i);
21 }
22 
23 /**
24 continue 语句(带有或不带标签引用)只能用在循环中。
25 break 语句(不带标签引用),只能用在循环或 switch 中。*/
26 
27 //通过标签引用,break 语句可用于跳出任何 JavaScript 代码块:
28 var myCar = ['car1', 'car2', 'car3', 'car4', 'car5'];
29 
30 list: {
31     console.log(myCar[0]);
32     console.log(myCar[1]);
33     console.log(myCar[2]);
34     break list;
35     console.log(myCar[3]);
36     console.log(myCar[4]);
37 }


8】、typeof、null、undefined

 1 /**
 2 * typeof操作符检测变量的数据类型
 3 *
 4 * Null
 5 * null 表示空对象的引用
 6 * typeof null为 object
 7 *
 8 * Undefined
 9 * undefined 是一个没有设置值的变量
10 * 任何变量都可以设置值为undefined来清空
11 *
12 * Undefined和Null的区别*/
13 
14 console.log(typeof('John'));//string
15 console.log(typeof(3.14));//number
16 console.log(typeof(false));//boolean
17 console.log(typeof([1,2,3,4]));//object,数组是一种特殊的对象类型
18 console.log(typeof({name: 'John', age: 34}));//object
19 
20 //Undefined和Null的区别一:
21 console.log(typeof(null));//object
22 console.log(typeof(undefined));//undefined
23 
24 var person = 'lqc';
25 //设置为null来清空对象
26 person = null;
27 
28 //设置为undefined来清空对象
29 person = undefined;
30 
31 var person2;
32 console.log(person2);//undefined
33 
34 //Undefined和Null的区别二:
35 console.log(undefined == null);//true
36 console.log(undefined === null);//false