Var, let and const are used to declare a variables in JavaScript. A variable is a container that holds a value.
DIFFERENCE BETWEEN VAR, LET AND CONST.
- Var has a function scope: this mean the variable is accessible throughout the entire functions regardless of the block it's in. Let has a blocked scope: this means that the variable is only accessible within the block it's declared it. This is the same with Const.
- Var allows re-assignment of the same variable multiple times within the same scope Let does not allow re-assignment of the same variable within the same scope Const does not allow re-assignment of the same variable within the same scope
Why is it recommended to const for JavaScript
- In const a variable declared cannot be re-assigned.This helps prevent unintended changes to the variable assigned.
- Prevents accidental re-assignment.
Top comments (1)
Hello @akinsanya_toluwanimi_e409
Allow me to correct a few things here:
Let
allows reassignment but does not allow redeclaration in the same scopeFor Instance:
but you cannot say:
Tips: