SOURCE

console 命令行工具 X clear

                    
>
console
//全局变量、局部变量
var globalVarlable1=0;
document.write(globalVarlable1+'</br>');
//document.write(localVarlable1);
function test(){
    var localVarlable1=2;
    document.write(localVarlable1+'</br>');

}

var globalVarlable2=1;
document.write(globalVarlable2+'</br>');
//document.write(localVarlable2);
function test1(){
    var localVarlable2=3;
    document.write(localVarlable2+'</br>');

}

function test2(){
    document.write(localVarlable3+'</br>');
    var localVarlable3=5;
    

}
test();
test1();
test2();

//对于全局和局部变量:输出可以在定义之前,未赋值
document.write(globalVarlable3);
var globalVarlable3=4;


document.write("<br>----------------------</br>");

//调用:1、直接调用 2、表达式调用(有返回值) 3、超链接调用(HTML文件 a标签)

function a1(){
    document.write('Welcome to my world'+'</br>');
}
a1();

function add(left,right){
    return left+right;
}
document.write(add(1,2)+3+'</br>');

function WelcomeMes(){
    alert('Welcome');

}
document.write("<br>----------------------</br>");
//嵌套函数:在一个函数中再写入一个函数,只能在函数内部调用
//内置函数、自定义函数

//案例:判断年份是否为闰年(1、普通年:可以被4整除不能被100整除 2、世纪年:能被400整除 )
function isLeapYear(year){
    if(parseInt(year/4)==parseFloat(year/4)&&parseInt(year/100)!=parseFloat(year/100)){
        //普通年
        return year+'是普通年';
    }
    else if(parseInt(year/400)==parseFloat(year/400)){
        //世纪年
        return year+'是世纪年';
    }
    else{
        //其他年份
        return year+'是其他年份';
    }
    
}

document.write(isLeapYear(2000)+'</br>');
document.write(isLeapYear(2020)+'</br>');
document.write(isLeapYear(2021)+'</br>');
document.write(isLeapYear(4000)+'</br>');

document.write("<br>----------------------</br>");
function compareMax(arr1){
    var Max=0;
    for(i=0;i<arr1.length;i++){
        if(arr1[i]>Max){
            Max=arr1[i];

        }

    }
    return Max;
}

var arr1=[2,8,3,6];
document.write('Max is  '+compareMax(arr1));
<!DOCTYPE html> 
<html>
<head>
    <title>javascript函数</title>
</head>
<body>
    <!-- 调用超链接 在函数调用前加javascript: -->
    <a href="javascript:WelcomeMes()">欢迎</a>
    <br>--------</br>
</body>
</html>