Declaring Variables
Basic JavaScript uses the keyword var
for variable declaration. ECMAScript 6 introduced two new keywords to declare variables; they are let
and const
. In the world of Professional JavaScript variable declaration, var
is now the weakest link. In this topic, we will go over the new keywords, let
and const
, and explain why they are better than var
.
The three ways to declare variables in JavaScript are by using var
, let
, and const
. All function in slightly different ways. The key differences between the three variable declaration keywords are the way they handle variable reassignment, variable scope, and variable hoisting. These three features can be explained briefly as follows:
Variable reassignment: The ability to change or reassign the variable's value at any time.
Variable scope: The extent or area of the code from which the variable may be accessed.
Variable hoisting: The variable instantiation and assignment time in relation to the...