1、if-else
if (time<10)
{
document.write("<b>早上好</b>");
}
else if (time>=10 && time<20)
{
document.write("<b>今天好</b>");
}
else
{
document.write("<b>晚上好!</b>");
}
2、switch
var d=new Date().getDay();
switch (d)
{
case 6:
x="今天是星期六";
break;
case 0:
x="今天是星期日";
break;
default:
x="期待周末";
}
document.getElementById("demo").innerHTML=x;
3、for
<script>
let str = 'abcdefg';
let arrays = [1, 2, "abc"];
//for
for (let i = 0; i < str.length; i++) {
console.log('for:'+str[i]);
}
//for of
for (let s of str) {
console.log('for of:'+s);
}
//for in
for (let key in arrays) {
console.log('for in:'+arrays[key]);
}
//forEach
arrays.forEach(element => {
console.log('forEach:'+element);
});
</script>
5、while
while
var i = 0;
while (i < names.length) {
console.log(names[i]);
i++;
}
do/while 循环
var i = 0;
do
{
x=x + "The number is " + i + "<br>";
i++;
}
while (i<5);
6、break
中止for
for (i=0;i<10;i++)
{
if (i==3) break;
x=x + "The number is " + i + "<br>";
}
7、continue
跳过这次for,继续下一个
for (i=0;i<=10;i++)
{
if (i==3) continue;
x=x + "The number is " + i + "<br>";
}
8、return
直接返回