In with let
Learn why let is preferred over var with examples.
We'll cover the following
let
is the sensible replacement for var
. Anywhere we used var
correctly before, we can replace it with let
. let
removes the issues that plague var
and is less error prone.
No redefinition
let
does not permit a variable in a scope to be redefined. Unlike var
, let
behaves a lot like variable definitions in other languages that strictly enforce variable declarations and scope. If a variable is already defined, using let
to redefine that variable will result in an error.
Example
Get hands-on with 1400+ tech skills courses.