javaScript流程控制语句

  • if-else
var a=10;
var b=0;
if(a>b){
    console.log(a);
}else{
    console.log(b);
}
//条件判断如果得到的不是boolean值,自动调用Boolean()方法
if(a){
    console.log(a);
}
if(b){
    console.log(b);
}else{
    console.log("0的Boolean值是false");//如果页面没定义中文字符集可会回打印乱码
}
  • do-while
var i=0
do{
    i+=3;
    console.log(i);
}while(i<10)
//结果:3   6   9  12 
  • while
var x=3;
while(x>0){
    console.log(--x);
}
//结果:2 1 0
  • for
for(var i=0 ;i<3;i++){
    console.log(i);
}
//结果:0  1  2
  • for-in
for(var property in window){
    console.log(property);
}
//打印window里面的每一个属性,
//最好做null和undefined判断,老的浏览器版本会有异常
  • with
//with仅仅是一种简化的操作,但是对于调试和性能来说并不好
console.log(location.search.substring(1));//
console.log(location.hostname);//
console.log(location.href);//
with(location){
    console.log(search.substring(1));//
    console.log(hostname);//
    console.log(href);//
}
  • switch,基本和c/java一样,后面要有break;否则会直接执行
var a=10;
switch(a){
    case 3:console.log(3);break;
    case 10:console.log(10);break;
    default:console.log(a);break;
}
a=9;
switch(a){
    case 3:console.log(3);break;
    case 10:console.log(10);break;
    default:console.log(a);break;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • d--- NaN是什么? 有什么特别之处? NaN 简单的理解是:NaN是一个数值,但具体不知道是多少我们先用ty...
    NinthG阅读 3,680评论 0 50
  • 不支持上传文件,所以就复制过来了。作者信息什么的都没删。对前端基本属于一窍不通,所以没有任何修改,反正用着没问题就...
    全栈在路上阅读 6,062评论 0 2
  • 语句 JavaScript程序的执行单位为行(line),也就是一行一行地执行。一般情况下,每一行就是一个语句。 ...
    米塔塔阅读 3,297评论 1 10
  • we will build one antibody production plant which consist...
    Luke_7f0a阅读 2,642评论 0 0
  • 昨天菱菱扭伤了脚,孩子很疼,应该说第一天很疼,我很着急,然后决然请假带孩子去医院,一路上抱着她,孩子很乖巧,一直说...
    其其菱菱阅读 1,310评论 0 0

友情链接更多精彩内容