JavaScript-primer

Variables and Scoping

Let

A keyword for declaring a block-scoped variable that cannot be accessed before initialization.

Var

A keyword for declaring a function-scoped variable that is automatically initialized to undefined when it is hoisted.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var

Const

A keyword for declaring a constant value. Constants have the same behavior as variables declared with let, except they cannot be reassigned.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/Const

Block Scope

The behavior of a variable that is only accessible inside of the block it was defined. Most of the time, the block will simply be the nearest pair of curly braces to the declaration.

Function Scope

The behavior of a variable that is accessible anywhere inside of the function it was defined.

Hoisting

The process by which the JavaScript engine moves variable declarations to the top of their scope, allocating memory for them before reaching the line of code where they are declared. For variables declared with var, they are initialized to undefined until reaching the line of code that initializes the variable. For variables declared with let or const, the variable is not initialized and thus cannot be accessed before the line of code that initializes it. For example:

console.log(testVar) // Undefined
console.log(testLet) // reference error 

var testVar = 10;
let testLet = 10; 

console.log(testVar) // 10 
console.log(testlet) // 10