Break 和 Continue 语句
break 它常用于跳出 switch() 语句, break 语句也可用于跳出循环。break 语句跳出循环后,会继续执行该循环之后的代码(如果有的话)
continue 语句中断循环中的迭代,如果出现了指定的条件,然后继续循环中的下一个迭代。
如:function liang(){
var aq1 = document.getElementById('box');
var hua = '';
for(var i = 0; i < 8; i++){
if(i == 5){
break; //满足条件之后中断循环 输出:0,1,2,3,4
// continue; //只是跳过满足条件的循环 输出:0,1,2,3,4,6,7
}
hua += '输出的是:' + i + '<br/>';
}
aq1.innerHTML = hua;
}
JavaScript 语句可以进行标记。如需标记 JavaScript 语句,请在语句之前加上冒号:
continue 语句(带有或不带标签引用)都只能用在循环中。就是说不管怎么样 continue 语句都只能在循环中使用。
break 语句(不带标签引用),只能用在循环或 switch 中。如果通过标签引用,break 语句可用于跳出任何 JavaScript 代码块。
如下图:
有了标签,可以使用break和continue在多层循环的时候控制外层循环。
如:outerloop: for(var i = 0; i < 10; i++){
innerloop: for(var j = 0; j < 10; j++){
if(j > 4){
break;
}
if(i == 2){
break innerloop; //这样相当于使用了Continue,跳出了i==2的循环,继续执行下一轮的循环
}
if(i == 6){
break outerloop;
}
console.log('输出i' + i + '<br/>' + '输出j' + '<br/>' + j + ' ');
}
}
typeof, null, undefined和valueOf
typeof 操作符
使用 typeof 操作符来检测变量的数据类型。
如:function liang(){
var box = document.getElementById('box');
box.innerHTML = typeof 'liang' + '<br/>' + // string
typeof(3.14) + '<br/>' + // number
typeof false + '<br/>' + // boolean
typeof [1,2,3,4,'liang'] +'<br/>' + // object 数组是一种特殊的对象类型, 因此 返回 object。
typeof {name : 'liang', age : 36, sss : 'zhong'}; // object
}
null
null是一个只有一个值的特殊类型。表示一个空对象引用。
用 typeof 检测 null 返回是object。
undefined
undefined 是一个没有设置值的变量。任何变量都可以通过设置值为 undefined 来清空。 类型为 undefined.
null 和 undefined 的值相等,但类型不等
如:box.innerHTML = 'null类型:' + typeof null + '<br/>' +
'undefined类型:' + typeof undefined + '<br/>' +
'全等?:' + (null === undefined) + '<br/>' +
'恒等?:' + (null == undefined);