1.条件语句
- if…else…
var price=1000;
if(price>2000)
{
console.log('expensive');
}
else if(price<2000&&price>500)
{
console.log('a proper price');
}
else
{
console.log('too cheap');
}
- switch语句
var choice=2;
switch(choice){
case 1:
console.log('this is 1');
break;
case 2:
console.log('this is 2');
break;
default:
console.log('this is a default choice');
break;
}
2.循环语句
- while语句
var loop=1;
while(loop<=10)
{
let sentence=`this is the ${loop}th loop<br/>`;
document.write(sentence);
loop++;
}
- do…while…
- for语句
for(var loop=1; loop<10; loop++)
{
document.write(`this is the ${loop}th loop<br/>`);
}
- continue/break
- for in 语句
var nameList={
"Tom":95,
"John":100,
"Sahra":86
};
for(student in nameList)
{
if(nameList.hasOwnProperty(student))
document.write(`${namelist.student}<br/>`);
}
3.异常捕获语句
try{
} catch(exception)
{
} finally{
}
[例]
try{
document.write(nameList);
}
catch(exception)
{
document.write(exception);
}
finally{
alert('The end of test!');
}
4.With 语句
var Kitty={
age:15,
friend:{
name:'Tom',
age:2
}
};
with(Kitty)
{
document.write(`${friend.name} ${friend.age} `);
}