# var vs let/const

## `var` vs `let/const`

The **let/const** keyword conceals the variable to its own lexical environment/scope. It provides data privacy because sometimes you want to *limit the use of a variable to a scope*.

The **var** keyword “hoists” (or lifts up) the variable definition (its name) up all the way to the ***global scope***. Regardless what ***block-scope*** your variable is defined in using **var**, it always becomes available at the uppermost scope.

Variables defined inside a ***function-scope*** using the **var** keyword are concealed to that function's scope.

For these reasons the **let** keyword is considered to be a “safer” alternative to the **var** keyword, even though in a few cases they produce exactly the same behavior. The difference is that **let/const** always conceals to the scope its defined in.

This is also true of ***callback*** functions (which are essentially are ***function-scope***.)

**const** is just like let, except that the value of a variable declared with const keyword cannot be changed.

During the 'hoisting' cycle of the code execution, when **var** is hoisted/moved up in the scope, its assigned an **'undefined'** value initially.

Thus, if a var variable is used before any value is assigned to it, it will have a value of 'undefined' (which will not cause any error if used in console log or compare expressios or few other cases)

**'uninitialized' value for let/const**&#x20;

**let/const** variables, defined in global or function scope (i.e. a scope for which an execution context is created in the JSRuntime's call-stack, not the scopes created by if/for/while statements) - such let/const variables are also hoisted up in the variable envionment of their respective execution context with a value of **'uninitialized'.**

When such 'uninitialized' variable is used before any value is assigned to it, javascript will throw an error - <mark style="color:red;">ReferenceError: cannot find value of uninitialized</mark> (check the exact error). This area of code where a let/const var cannot be used until assigned a value is called **Temporal Dead Zone(TDZ)**.
