A lot of shiny new features came out with ES2015 (ES6). And now, since it’s 2020, it’s assumed that a lot of JavaScript developers have become familiar with and have started using these features.
While this assumption might be partially true, it’s still possible that some of these features remain a mystery to some devs.
One of the features that came with ES6 is the addition of let
and const
, which can be used for variable declaration. The question is, what makes them different from good ol’ var
which we’ve been using? If you are still not clear about this, then this article is for you.
In this article, we’ll discuss var
, let
and const
with respect to their scope, use, and hoisting. As you read, take note of the differences between them that I’ll point out.
Var
Before the advent of ES6, var
declarations ruled. There are issues associated with variables declared with var
, though. That is why it was necessary for new ways to declare variables to emerge. First, let’s get to understand var
more before we discuss those issues.
Let
let
is now preferred for variable declaration. It’s no surprise as it comes as an improvement to var
declarations. It also solves the problem with var
that we just covered. Let’s consider why this is so.
Const
Variables declared with the const
maintain constant values. const
declarations share some similarities with let
declarations.
https://www.freecodecamp.org/news/var-let-and-const-whats-the-difference/
Destructuring assignment: JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into a bunch of variables.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment