Articles β†’ JAVASCRIPT β†’ let in JavaScript

let in JavaScript






Purpose





β€œlet” cannot be redeclared




<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script>
        alert("Hi");


        let x = 12;
        let x = "Karan";

        alert(x);


        var y = 12;
        var y = "Karan";

        alert(y);



    </script>
</head>
<body>

</body>

</html>





Block scope






<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script>

        {
            let x = 2
            var y = 4;
        }
        //  This code will give error
        alert(x);
        //  This code will run successfully
        alert(y);

    </script>
</head>
<body>

</body>

</html>





Posted By  -  Karan Gupta
 
Posted On  -  Friday, July 30, 2021

Query/Feedback


Your Email Id  
 
Subject 
 
Query/FeedbackCharacters remaining 250