Member-only story
JavaScript, the language that powers the web, has evolved over the years, and ES6 (ECMAScript 2015) introduced several features that make our lives as developers easier and our code more robust.
In this article, we’ll shine a light on two essential additions: let
and const
. These seemingly simple keywords bring a breath of fresh air to variable declarations, offering clarity and flexibility. So, let's dive in.
The Rise of let
and const
In the good old days of JavaScript, the only way to declare variables was using var
. While var
is still around, ES6 introduced let
and const
to provide developers with more control over variable scoping and immutability.
let
: Embracing Block Scope
The introduction of let
marked a shift towards a more predictable scoping mechanism in JavaScript. Unlike var
, which is function-scoped, let
is block-scoped. What does that mean? Well, variables declared with let
are confined to the block, statement, or expression where they are defined.
function exampleLetScope() {
if (true) {
let x = 10;
console.log(x); //…