0
0
Javascriptprogramming~5 mins

Variable declaration using let in Javascript

Choose your learning style9 modes available
Introduction

We use let to create a variable that can change later. It helps us store and update information in our program.

When you want to store a value that might change, like a score in a game.
When you need a variable only inside a small part of your code, like inside a loop.
When you want to avoid mistakes by limiting where a variable can be used.
When you want to update a variable step by step, like counting clicks.
When you want clearer and safer code compared to older ways of declaring variables.
Syntax
Javascript
let variableName = initialValue;

let creates a variable that can be changed later.

You can declare a variable without giving it a value first: let x;

Examples
This creates a variable named age and sets it to 25.
Javascript
let age = 25;
Here, score is declared first without a value, then given the value 10.
Javascript
let score;
score = 10;
You can change the value of count after declaring it.
Javascript
let count = 0;
count = count + 1;
Sample Program

This program shows how a let variable can change its value and print both values.

Javascript
let name = "Alice";
console.log(name);

name = "Bob";
console.log(name);
OutputSuccess
Important Notes

Variables declared with let are block-scoped, meaning they only exist inside the nearest curly braces { }.

Using let helps avoid errors that happen when variables are used outside their intended area.

Summary

let creates variables that can change value.

It limits where the variable can be used to keep code safe and clear.

Use let instead of older var for better coding habits.