let
The let
keyword is used to declare a **block-scoped variable**, introduced in ES6 (ECMAScript 2015). Unlike var
, variables declared with let
are not hoisted to the top of the block and are safer to use.
let x = 10; x = 20; // Allowed, value updated
Click the button to see how let
works:
let
:{ let a = 5; console.log(a); // 5 } // console.log(a); // Error: a is not defined
let x = 10; let x = 20; // β Error: Identifier 'x' has already been declared
Use let
when you expect the variableβs value to change later.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!