JS既在client side有,也在server side 和 database side有。
一般情况,js代码可以放在任何位置。 js库一般放在head里面 js代码一般放在body的最后面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<!-- LOAD JS LIBRARIES HERE -->
<scrpit src="mycode.js"></scrpit>
</head>
<body>
<!-- your js code here usually -->
<script>
function surprise() {
alert("hello");
}
</script>
</body>
</html>
number 只有一种表示,可以用科学计数法。
string
boolean && || !
object
var typeof
parseInt parseFloat String
[] length join push shift pop unshift concat
var a = [1,2,3];
var a = new Array(3);
array.join(separator)
array.length
array.push
array.pop
array.shift
array.unshift
array1.concat(array2)
onload is triggered when the object is loaded.
<body onload="alert('hello');
alert('hello2')">
alert confirm prompt isNaN
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script>
alert("alert");
confirm("confirm");
var user_name;
user_name = prompt("what's your name");
document.write("welcome to my page "
+ user_name +"!");
</script>
</body>
</html>
random
Math.random Math.floor
Math.random()*max_value #[0,max_value)
function
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script>
function user_age() {
age = prompt("input your age");
return parseInt(age)
}
function check_age() {
if (user_age() < 18)
alert("go out")
}
</script>
</head>
<body onload="check_age()">
</body>
</html>
if else switch break
while () {}; string.indexof(“text”) location of “text”
do {} while(); does the once at least
local and global variable
如果在函数里面使用了没有被var创建的变量a,a会变成全局变量。
使用chrome中的开发者工具 同时可以在console中输入js代码
console.log()与alert()的区别是用户不可见但是开发者可见。