Var, Let, and Const

Posted under » JavaScript on 29 October 2009

var declarations ruled since the beginning. There are issues associated with variables declared with var, though. That is why it was necessary for new ways to declare variables to emerge.

Scope essentially means where these variables are available for use. var declarations are globally scoped or function/locally scoped.

var greeter = "hey hi";

function newFunction() {
        var hello = "hello";
    }

Here, greeter is globally scoped because it exists outside a function while hello is function scoped. So we cannot access the variable hello outside of a function.

One thing I like about var is that variables can be re-declared and the values updated.

Hoisting is a JavaScript mechanism where variables and function declarations are moved to the top of their scope before code execution. This means that if we do this:

var greeter = "hey hi";

function newFunction() {
        var hello = "hello";
    }

web security linux ubuntu Raspberry   git   javascript python django Laravel drupal php apache mysql  MongoDB AWS data  ML AI