javascript变量、函数的定义
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>demo1</title>
</head>
<body>
<script>
//使用关键字var定义一个变量
var myNum1 = 10;
alert(myNum1);
//使用关键字function定义一个函数
function disPlay(num) {
console.log(num);
}
//调用刚创建的display函数,并传入实参
disPlay(myNum1);
</script>
</body>
</html>
javascript流程控制if else switch
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo3</title>
</head>
<body>
<script>
/*
小明妈妈和小明说:如果你考到90分以上,我就给你买汽车玩具,考到60分到90分之间
就带你去吃肯德基,如果你考60分以下就回来挨打
*/
function mamaSay(grade) {
if(grade>=90){
alert("买玩具汽车");
}
if(grade<90&&grade>=60){
alert("去吃肯德基");
}
if(grade<60){
alert("回家挨打!");
}
}
function mamaSay2(grade) {
if (grade >= 90) {
alert("买玩具汽车");
} else if (grade < 90 && grade >= 60) {
alert("去吃肯德基");
} else {
alert("回家挨打!");
}
}
function mamaSay3(grade) {
/*不知为何,我在case中放入表达式会走默认。所以
我就把grade强制转换为int类型了*/
grade=parseInt(grade/10);
switch (grade) {
case 10:
case 9:
alert("买玩具汽车");
break;
case 8:
case 7:
case 6:
alert("去吃肯德基");
break;
default:
alert("回家挨打!");
}
}
var mygrade = 90;
mamaSay3(mygrade);
</script>
</body>
</html>
javascript三种循环
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo4</title>
</head>
<body>
<script>
var arr = [12,15,17,64,84,61,24,56];
for(i=0;i<arr.length;i++){
console.log(arr[i]);
}
var i = 0;
while (i<arr.length){
console.log(arr[i]);
i++
}
i = 0;
do {
console.log(arr[i]);
i++;
}while (i<arr.length);
</script>
</body>
</html>
- 数据类型转换:parseInt、isNaN函数的使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>demo5</title>
</head>
<body>
<script>
var number = "135";
number = parseInt(number);
console.log(number.constructor===Number); //控制台显示ture,转换成功
var str = "西门大官人";
console.log(isNaN(number));//控制台显示false,表示变量number是数字,js自动转换为数字了
console.log(isNaN(str));//控制台显示ture,表示变量str不是数字
</script>
</body>
</html>