1、for循环
// 定义数组
let students=["张三","李四","王五"];
//console.log(typeof students);// object
// 遍历数组元素
for (let i = 0; i < students.length; i++) {
document.write(students[i] + "<br>");
}
2、do循环
let names=["tom","jack","lily"];
let i=0;
do{
document.write(names[i] + "<br>");
i++;
}while(i<names.length)
3、for...颜色等
let names=["tom","jack","lily"];
// for..in相当于c#的foreach
// for..in循环遍历数组
for (let i in names) { // i是下标
document.write(names[i] + "<br>");
}
// for..in遍历对象的属性
for (let p in document) {
document.write(p + "<br>");
}
// 网页文档标题
// document.title="hello js";
// 设置文档的背景色
// document.bgColor="#ccc";
4、函数运算符
<script>
let a=10;
a+=20;
document.write(a + "<br>");
a-=15;
document.write(a + "<br>");
a*=3;
document.write(a + "<br>");
a/=20;
document.write(a + "<br>");
a%=5;
document.write(a + "<br>");
let c=2,b=1;
let tmp=b;
b=c;
c=tmp;
</script>
5、算数运算符
// +:加法、字符串连接
let a=1+2; // 加法
let b="1"+2; // 连接
document.write("数值b=" + b);
document.write("<br>");
// ** : 两个乘号,表示乘方
let c=2**10;
document.write("c=" + c + "<br>");
// 除法
let d=10/3;
document.write("d=" + d + "<br>");
// 求余数
let e=10%3; // e=1
document.write("e=" + e + "<br>");
// 自增++、增减--
e++; // e=e+1或e+=1
++e;
document.write("e=" + e + "<br>");
//e=3
let g=(e++)+10;
document.write("g=" + g + "<br>");
6、关系运算符
<script>
let a=1;
let b="1";
// == 只判断表面数值,不判断类型
document.write(a==b);// true
// === 既比较数值,也比较类型( 建议使用===)
document.write(a===b);// false
</script>