Sunday, 11 July 2021

JavaScript tutorial #4 | Difference between var , let , const @echocoding | soham kawde

source code

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Differece between var , let and const keywords</title>
</head>
<body>
    <script>
        var person = "soham";
        var person = "programmer";
        // console.log(person);
        let person_1 = "Rohan";
        // let person_1 =  "Web developer"; we can not re-declared the let variable again in same scope
        const number = 1;
        // number = 2; we can not re-declared the const variable again in same scope
        if (0<1){
            var person = "website developer";
            let person_1 = "web Developer";
            const number = 2;
            console.log(number);
            // console.log(person_1);
        }
        console.log(number);
        console.log(person);
        // console.log(person_1);
    </script>
</body>
</html>

Difference between var , let , const var can reassigned values in same block scope or different block of scope , although It can assign values befor declared variables (hoisting is possible). let can not reassigned values in same block scope but It can assign values in different block of scope. Hoisting is not possible(In let , variable can not assign values before let declaration). const can not reassigned values in same block of scope but it can assign values in different block of scope. Hoisting is not possible (In let , variable can not assign values before let declaration).

 

No comments:

By laptop