JavaScript运算符
JavaScript条件语句
JavaScript循环语句
JavaScript跳转语句
JavaScript运算符
1,算数运算符:
+,-,*,%,/,++,--
<body>
<p>i = 10, j = 10, i+j = ?</p>
<p id="sumid"></p>
<button οnclick="mysum()">结果</button>
<script>
function mysum(){
var i = 10;
var j = 10;
var m = i+j;
document.getElementById("sumid").innerHTML = m;
}
</script>
</body>
2,赋值运算符:
=,+=,-=,*=,/=,%=
3,字符串操作
4,比较运算符:
==,===,!=,!==,>,<,>=,<=
<!--三个等号,类型相同,内容相同-->
<script>
var i=10;
var j=10;
document.write(i===j);
</script>
结果:<script> var i="10"; var j=10; document.write(i==j); </script>
5,逻辑运算符:
&&,||,!
6,条件运算符:
例如:x<10?“x比10小”:“x比10大”
JavaScript条件语句
1,if...else
<script>
var i = 11;
if(i>10){
document.write("nihao");
}else{
document.write("dajiahao");
}
</script>
2,switch
<script>
var i=0;
switch (i){
case 1:
document.write("11");
break;
case 2:
document.write("22");
break;
default:
document.write("default");
break;
}
</script>
JavaScript循环语句
1,for循环,for/in
<pre name="code" class="javascript"><script>
var i=[1,2,3,4,5,6];
for(var j=0;j<6;j++){
document.write(i[j]+"、");
}
</script>
for/in
<script>
var i=[1,2,3,4,5,6];
var j;
for(j in i){
document.write(i[j]);
}
</script>
2,while循环,do...while
<script>
var i=10;
while(i>9){
document.write(i);
i--;
}
</script>
do...while
<script>
var i=10;
do{
document.write(i);
i--;
}while(i>9)
</script>
JavaScript跳转语句
1,break
<script>
for(var i=0;i<10;i++){
if(i==5){
break;
}
document.write("i="+i+"<br />");
}
</script>
2,continue
<script>
for(var i=0;i<10;i++){
if(i==5){
continue;//跳出本次循环,进行下次循环。
}
document.write("i="+i+"<br />");
}
</script>