引入方式
1.内部js
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript</title>
</head>
<body>
<script>alert("hello world!")</script>
</body>
</html>
2.外部作为一个单独的js文件,引入
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript</title>
</head>
<body>
<script src="hello.js"></script>
</body>
</html>
hello.js:
alert("hello JavaScript!")
3.行内js,内联到了标签内
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript</title>
</head>
<body>
<p onclick="alert('hello world!')">我是文本,但是你点我一下试试</p>
</body>
</html>
alert()会弹出框,可以使用console.log();在控制台看到反馈
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript</title>
</head>
<body>
<script>
console.log('hello console!')
</script>
</body>
</html>
定义变量var let
var 变量名=初始值(老式写法)
let 变量名=初始值(新式写法,推荐)
没有类型不代表没有类型,取决于初始值。