JS Tutorial



JS LET


JavaScript 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.

πŸ“ Syntax:

let x = 10;
x = 20; // Allowed, value updated

πŸ§ͺ Try it Live:

Click the button to see how let works:

πŸ“¦ Features of let:

  • Block Scoped: Works only inside the block it’s declared in (like if, loops, etc.)
  • Can be updated: You can change the value later
  • Cannot be re-declared: In the same scope, re-declaring causes an error

🧠 Example:

{
  let a = 5;
  console.log(a); // 5
}
// console.log(a); // Error: a is not defined

🚫 Re-declaration Error:

let x = 10;
let x = 20; // ❌ Error: Identifier 'x' has already been declared

πŸ“ Best Practice:

Use let when you expect the variable’s value to change later.


🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review