Difference between Var, Let, and Const in JavaScript

So a month ago I start restudying JavaScript because I was working with his frameworks without mastering the basics.. so I decided to share my journey, its just my notes if you have observations you are very welcome.

Var:

Scope of var, any var declaration outside the function can be used in the whole window.
Any var declaration inside the function can be used just in the function.
Var variables can be re-declared and updated WITHOUT GETTING ERROR

Example:

    var one = "HEY"; // used in the whole window
    
    function myFunction() {
        var two = "hey 2"; // used inSIDE THE FUNCTOION
    }

Let:

When you declare a variable with let, this variable is available to use inside that block of code only.
* This is a block of code {…}

Example:

let salam = "salamo alaykom";
let times = 4;
if ( times > 3 ) {
    let salam = "ahlaaan";
    console.log(salam); // "ahlaaan"
}
console.log(salam) // salam is not defined.

Const:

Variables declared with the const maintain constant values

Hope this small post is helpful for you.. See you again..

Leave a Reply

Your email address will not be published. Required fields are marked *