javascript中var与let的区别

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>


</head>
<body>
<script>

    // let与var的区别
    // 1.只在代码块中有效
    for (var i = 0; i < 10; i++) {

    }
    console.log(i)//打印10
    for (let i = 0; i < 10; i++) {

    }
    console.log(i)//报错

    //不能重复声明
    var a=1
    var a=2

    let b=1
    let b=2  //报错

    //不存在变量提升
    console.log(c)
    var c="apple"

    console.log(d)
    let d="banana"


</script>
</body>
</html>

posted @ 2024-07-28 17:03  Bre-eZe  阅读(4)  评论(0编辑  收藏  举报