JavaScript语法-特殊语法、流程控制语句
JavaScript语法-特殊语法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>特殊语法</title> <script> // 1、语句以;结尾.如果一行只有一条语句则;可以省略(不建议) var a = 3; alert(a); /** * 2、变量的定义使用var关键字,也可以不使用 * 用:定义的变量是局部变量 * 不用:定义的变量是全局变量(不建议使用) */ /* b = 4; alert(b);*/ var b; function f() { b = 4; } f(); alert(b); </script> </head> <body> </body> </html>
JavaScript语法-流程控制语句
1.if...else..
2.switch
- 在java中,switch语句可以接受的数据类型:byte int shor char,枚举(1.5),String(1.7)
- switch(变量):
- case 值:
- switch(变量):
- 在JS中,switch语句可以接受任意的原始数据类型
3.while
4.do...while
5.for
代码案例:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>switch语法</title>
<script>
var a = "abc";
switch (a) {
case 1:
alert("number");
case "abc":
alert("string");
case true:
alert("true");
case null:
alert("null");
case undefined:
alert("undefined");
}
// while
var sum=0;
var num=1;
while(num <=100){
sum +=num;
num ++;
}
document.write(sum+"<br>");
// for
var z=0;
for(var i=1;i<=100;i++){
z +=i;
}
document.write(z);
</script>
</head>
<body>
</body>
</html>
运行结果



浙公网安备 33010602011771号