1、prompt-弹出一个输入框
任意打开浏览器页面->更多工具->开发者工具->控制台
2、alert-弹出一个警示对话框
3、console.log-在控制台打印一个日志
如何进行JS编写?(关联在HTML中执行)
1)内部方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>演示内部 JS</title>
</head>
<body>
<script>alert("Hello World");</script>
</body>
</html>
2)外部方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>外部 JS</title>
</head>
<body>
<script src="script.js"></script>
</body>
</html>
alert("你好世界")
注:主要使用外部方式来开发web应用
JS中的变量和数据类型
var name = prompt("请输入姓名:");
var age = prompt("请输入年龄:");
var score = prompt("请输入分数:");
var a = prompt("请输入一个数字");
if (isNaN(a)) {
alert("请输入数字");
} else {
if (a % 2 === 0) {
alert(`${a} 是一个偶数`);
} else {
alert(`${a} 是一个奇数`);
}
}
输入12时
console.log(name);
console.log(age);
console.log(score);
var a = [1, 5, 3, 7, 2, 4, 6, 9, 8, 19];
function compare(first, second) {
return first - second;
}
a.sort(compare);
console.log(a);