前端学习(434):变量的作用域

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>变量的作用域</title>
</head>
<body>
    <!-- 全局变量:直接在script直接使用var定义的变量
        局部变量:在函数中定义的变量
     -->
     <script>
     var s1=10;
     function fn2(){
        //  如果函数中定义变量,都是局部变量 
         var s2=20;
         console.log(s2);
         window.s3+=10;
         console.log(s3);
     }
     fn2();
     console.log(s1);
     </script>
</body>
</html>

运行结果

发布了1602 篇原创文章 · 获赞 705 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/weixin_43392489/article/details/104032588