Search⌘ K
AI Features

In with let

Explore how the let keyword enhances JavaScript variable handling by enforcing block scope and prohibiting variable redefinition. Understand its advantages over var, including safer coding practices and clearer, error-proof code organization within blocks and functions. Practice using let to improve your modern JavaScript coding skills.

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

Javascript (babel-node)
'use strict';
//BROKEN_CODE
let max = 100;
console.log(max);
let max = 200;
console.log(max);

Explanation

This example is identical to the one we covered in the previous lesson, except that var has ...