测开之路九十八:js变量和语句

 

这里为了方便调试,在jsbin网站上面编写js脚本:https://jsbin.com/?js,console

可以点击增加/减少对应展示分页,Console为控制台部分,Output为页面部分

 

变量和运算

if语句

 

switch case

 

while

 

do while

 

for循环

 

for in

 

var a = 10;
var b = 20.5;
console.log(typeof a);
console.log(a+b);
console.log(a>b);
console.log(a<b && a>2);

 

if(typeof a == "number"){
  document.write("条件成立");
  console.log("条件成立");
}else{
  console.log("条件不成立");
}

 

//switch case
var grade = 'X'
switch(grade)
{
  case "A": document.write("成绩为A");
  break;

  case "B": document.write("成绩为B");
  break;

  case "C": document.write("成绩为C");
  break;

  default: document.write("未知成绩");

}

 

//while
var count = 0;
while (count < 10){
  document.write("当前值是:" + count + "<br />");
  count++;
}
document.write("循环结束")

 


//do while
var count = 0;
do{
  document.write("当前值是:" + count + "<br />");
  count++;
}
while(count < 10)

 

//for
for(var i=1; i<10; i++){
  document.write("当前值是:" + i + "<br />");

  //嵌套
  if(i == 8){
    break;
  }
}
document.write("循环结束");

 

//for in
var values = ["a", "b", "c", "d"];
for (value in values){
  //遍历索引
  document.write("当前索引是:" + value + "<br />");

  //遍历值
  document.write("当前值是:" + values[value] + "<br />");
}
document.write("循环结束");

posted @ 2019-08-27 15:26  向前走。  阅读(160)  评论(0编辑  收藏  举报