JS的变量命名:使用var定义变量,JS统一使用var定义数字(整数,小数),字符串,布尔类型,undefined,null。
通用语法:var 变量名=值;
规则:1、首字母建议以英文字母,_,$开头,不能使用数字和特殊字符(如 ., ?)
2、第二位可以用字母,_,$,数字
3、不能用系统关键字,保留字作为变量名(比如 for,while。。。。)
JS的数据类型:数字(整数,小数),字符串,布尔类型,undefined,null。
表达式:a+b>c
语句:c=a+b;
(1)if..else
if(){
}else{
}
示例1:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">e
<title>js基础语法</title>
</head>
<body>
<script type="text/javascript">
//if...else示例
var a=3,b=6;
if(a>b){
alert(a);
}else{
alert(b);
}
</script>
</body>
</html>
(2)多条件
if(){
}else if(){
}...else{
}
示例2:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js基础语法</title>
</head>
<body>
<script type="text/javascript">
//if...else if()...
var score=85;
if(score<60){
alert("不及格");
}else if(score>=60 && score<80){
alert("良好");
}else if(score>=80 && score<=100){
alert("优秀");
}
</script>
</body>
</html>
(3)switch多条件
switch(){
case 值:语句;break:
case 值:语句;break:
...
default 值
}
示例3:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>js基础语法</title>
</head>
<body>
<script type="text/javascript">
//switch(){case}...多条件
var date=3;
switch(date){
case 1:alert("星期一");break;
case 2:alert("星期二");break;
case 3:alert("星期三");break;
case 4:alert("星期四");break;
// 567省略。。。。。
default:alert("出错了");
}
</script>
</body>
</html>
(4)while循环:
while(){
}
(5)do...while循环
do{
}while();
(6)for循环
for(){}