console
var globalVariable1 = 0;
function test1() {
document.write(localVariable1+ "<br/>");
var localVariable1 = 2;
document.write(globalVariable1 + "<br/>");
document.write(localVariable1+ "<br/>");
}
document.write(globalVariable2 + "<br/>");
test1();
var globalVariable2 = 1;
function test2() {
var localVariable2 = 3;
}
test2();
document.write(globalVariable1 + "<br/>");
function welcomeMes(){
alert("Welcome");
}
function add(left, right) {
return left + right;
}
document.write((add(1, 2) + 3) + "<br/>")
function isLeapYear(year) {
var result = false;
if ((year % 4 == 0) && (year % 100 != 0)) {
result = true;
}
if (year % 400 == 0) {
result = true;
}
if (result) {
document.write( year + " is a leap year. <br/>");
}
else {
document.write( year + " is not a leap year. <br/>");
}
}
isLeapYear(2020);
isLeapYear(2000);
isLeapYear(2001);
function getMax(a,b,c,d) {
var maxNum;
maxNum = (a > b) ? a : b;
maxNum = (maxNum > c) ? maxNum : c;
maxNum = (maxNum > d) ? maxNum : d;
return maxNum;
}
document.write("4个数中的最大值为:" + getMax(1, 2, 4, 3));
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Javascript函数</title>
</head>
<body>
<div>
<a href="javascript:welcomeMes()">Welcome</a>
</div>
</body>
</html>