Skip to content

learnen.dinajpuritpark.com

Menu
Menu
JavaScript Variables Learn let and const with Practical Examples — Web Development Class 42

JavaScript Variables: Learn let and const with Practical Examples — Web Development Class 42

Posted on August 30, 2026

Class 042 — Variables: let, const

JavaScript programs need a way to store values so that those values can be used later.

Variables allow us to give names to values and use those names throughout our program.

In modern JavaScript, two important keywords for declaring variables are:

let
const

The main concept of this class is:

let creates a reassignable binding.

const creates a binding that cannot be reassigned.

As a general practice, use const by default when reassignment is not required, and use let when the variable needs to receive a new value later.

LEARNING OBJECTIVES

After completing this class, you will be able to:

  • Understand the basic concept of JavaScript variables.
  • Understand variable declaration.
  • Understand variable initialization.
  • Use let.
  • Use const.
  • Understand reassignment.
  • Understand why const cannot be reassigned.
  • Choose between let and const.
  • Follow basic variable naming rules.
  • Identify common mistakes.
  • Explain simple variable code.
  • Complete practical variable exercises.
  • Answer basic interview questions about let and const.

WHAT IS A VARIABLE?

A variable is a named reference used to work with a value in a program.

Example:

let age = 25;

Here:

let

declares the variable.

age

is the variable name.

=

assigns a value.

25

is the assigned value.

Now we can use:

age

instead of repeatedly writing the value directly.

Example:

let age = 25;

console.log(age);

Output:

25

WHY DO WE NEED VARIABLES?

Suppose we want to store a user’s name.

Without a variable:

console.log(“Rahim”);

With a variable:

const userName = “Rahim”;

console.log(userName);

Output:

Rahim

The variable gives the value a meaningful name.

Variables are useful for storing values such as:

  • Name
  • Age
  • Price
  • Quantity
  • Score
  • Username
  • Email
  • Product name
  • Total amount
  • Application state

VARIABLE DECLARATION

Creating a variable is called declaration.

Example:

let age;

Here:

age

has been declared.

We have not explicitly assigned a value yet.

VARIABLE INITIALIZATION

Giving an initial value to a variable is commonly called initialization.

Example:

let age = 25;

Here:

age

is declared and initialized with:

25

DECLARATION FIRST, ASSIGNMENT LATER

With let, you can declare a variable first:

let age;

Then assign a value:

age = 25;

Complete example:

let age;

age = 25;

console.log(age);

Output:

25

USING LET

Use let when the binding needs to be reassigned later.

Example:

let score = 10;

console.log(score);

Output:

10

Now change the value:

score = 20;

console.log(score);

Output:

20

Complete example:

let score = 10;

console.log(score);

score = 20;

console.log(score);

Output:

10
20

This is called reassignment.

WHAT IS REASSIGNMENT?

Reassignment means assigning a new value to an existing variable.

Example:

let price = 100;

price = 150;

First:

price = 100

Later:

price = 150

The binding now refers to the new value.

PRACTICAL LET EXAMPLE

Imagine a game score.

let score = 0;

console.log(score);

Output:

0

The player earns points:

score = 10;

console.log(score);

Output:

10

The player earns more points:

score = 20;

console.log(score);

Output:

20

Because the score changes, let is appropriate.

ANOTHER LET EXAMPLE

let stock = 10;

console.log(stock);

Output:

10

After selling one product:

stock = 9;

console.log(stock);

Output:

9

Because the value needs to change, let can be used.

USING CONST

Use const when the binding should not be reassigned.

Example:

const siteName = “WebDev Academy”;

console.log(siteName);

Output:

WebDev Academy

Trying to reassign it:

siteName = “Another Website”;

causes an error.

CONST REASSIGNMENT

Example:

const country = “Bangladesh”;

country = “Japan”;

This is not allowed.

The const binding cannot be reassigned.

CORRECT CONST EXAMPLE

const country = “Bangladesh”;

console.log(country);

Output:

Bangladesh

If the value does not need to be reassigned, const is usually a good choice.

LET VS CONST

let:

Can be reassigned.

Example:

let score = 10;

score = 20;

Valid.

const:

Cannot be reassigned.

Example:

const siteName = “My Website”;

siteName = “New Website”;

Invalid reassignment.

SIMPLE RULE

Ask yourself:

Will this variable need to be reassigned?

If NO:

Use const.

If YES:

Use let.

Example:

const userName = “Karim”;

let score = 0;

Here:

userName

does not need reassignment.

score

may change.

DEFAULT TO CONST

A useful modern JavaScript practice is:

Use const by default.

Use let when reassignment is actually necessary.

Example:

const productName = “Laptop”;

const price = 50000;

let stock = 10;

If productName and price are not going to be reassigned, const is appropriate.

If stock needs to change, let is appropriate.

IMPORTANT DIFFERENCE

Do not confuse:

Reassignment

with:

Changing the contents of an object or array.

Example:

const user = {
name: “Rahim”
};

This is allowed:

user.name = “Karim”;

But this is not allowed:

user = {
name: “Hasan”
};

Why?

Because const prevents reassignment of the variable binding.

It does not automatically make an object immutable.

CONST WITH OBJECT

Example:

const product = {
name: “Keyboard”,
price: 1500
};

Change a property:

product.price = 1700;

This is allowed.

Now:

console.log(product.price);

Output:

1700

But:

product = {
name: “Mouse”,
price: 800
};

is not allowed because it attempts to reassign product itself.

CONST WITH ARRAY

Example:

const products = [
“Keyboard”,
“Mouse”
];

Changing the array contents can be allowed.

Example:

products.push(“Monitor”);

Now the array contains:

Keyboard
Mouse
Monitor

But this:

products = [
“Laptop”
];

is reassignment and is not allowed.

IMPORTANT

You will study objects and arrays properly in later classes.

For now, remember:

const prevents reassignment of the binding.

It does not mean every value stored through const becomes completely unchangeable.

CONST MUST BE INITIALIZED

This is invalid:

const country;

A const declaration requires an initializer.

Correct:

const country = “Bangladesh”;

With let:

let country;

is allowed.

VARIABLE NAMING

Variables should have meaningful names.

Good:

const userName = “Rahim”;

const productPrice = 1500;

let totalScore = 0;

Poor:

const x = “Rahim”;

const a = 1500;

let q = 0;

Short names can be appropriate in limited contexts, but descriptive names usually make application code easier to understand.

JAVASCRIPT VARIABLE NAMING RULES

Variable names can contain:

  • Letters
  • Digits
  • Underscore _
  • Dollar sign $

But a variable name cannot normally start with a digit.

Valid:

let userName;

let user1;

let _total;

let $price;

Invalid:

let 1user;

VARIABLE NAMES ARE CASE-SENSITIVE

These are different identifiers:

let userName = “Rahim”;

let username = “Karim”;

JavaScript treats:

userName

and:

username

as different names.

CAMELCASE

A common naming convention in JavaScript is camelCase.

Example:

userName

productPrice

totalAmount

firstName

shoppingCart

The first word begins with lowercase.

Each following word begins with an uppercase letter.

Example:

const firstName = “Rahim”;

const productPrice = 5000;

let totalAmount = 0;

MEANINGFUL VARIABLE NAMES

Avoid:

let a = 500;

let b = 2;

let c = a * b;

Better:

const productPrice = 500;

const quantity = 2;

const totalPrice =
productPrice * quantity;

Now the purpose of the code is easier to understand.

USING VARIABLES WITH CONSOLE.LOG()

Example:

const name = “Rahim”;

console.log(name);

Output:

Rahim

Multiple variables:

const firstName = “Rahim”;

const age = 25;

console.log(firstName);

console.log(age);

Output:

Rahim
25

CALCULATIONS WITH VARIABLES

Example:

const price = 1000;

const quantity = 3;

const total =
price * quantity;

console.log(total);

Output:

3000

This is more useful than writing:

console.log(1000 * 3);

because the values now have meaningful names.

PRACTICAL SHOP EXAMPLE

const productName = “Keyboard”;

const unitPrice = 1200;

let stock = 10;

console.log(productName);

console.log(unitPrice);

console.log(stock);

Output:

Keyboard
1200
10

After one unit is sold:

stock = 9;

console.log(stock);

Output:

9

Here:

productName

const

unitPrice

const

stock

let

because stock needs reassignment.

PRACTICAL CART EXAMPLE

const productPrice = 1500;

let quantity = 1;

let totalPrice =
productPrice * quantity;

console.log(totalPrice);

Output:

1500

Now:

quantity = 3;

totalPrice =
productPrice * quantity;

console.log(totalPrice);

Output:

4500

Because quantity and totalPrice are reassigned, let is appropriate for them.

USING ONE VARIABLE WITH ANOTHER

Example:

const firstName = “Rahim”;

const lastName = “Ahmed”;

const fullName =
firstName + ” ” + lastName;

console.log(fullName);

Output:

Rahim Ahmed

The value of one variable can be used when creating another value.

CHANGING A LET VARIABLE

Example:

let balance = 1000;

console.log(balance);

balance = 800;

console.log(balance);

balance = 1200;

console.log(balance);

Output:

1000
800
1200

The same let binding receives different values over time.

DO NOT REDECLARE LET IN THE SAME SCOPE

Example:

let age = 20;

let age = 30;

This causes an error when both declarations are in the same scope.

Instead:

let age = 20;

age = 30;

The second line is reassignment, not redeclaration.

DO NOT REDECLARE CONST IN THE SAME SCOPE

Example:

const country = “Bangladesh”;

const country = “Japan”;

This is also invalid in the same scope.

Instead, use a different variable name if the values represent different things.

SCOPE

let and const are block-scoped.

Example:

{
let message = “Hello”;

const language = "JavaScript";

console.log(message);

console.log(language);

}

The variables belong to that block.

Trying to use them outside that block:

console.log(message);

will not work.

You will study scope in much more detail in a later JavaScript class.

BLOCK

A block is commonly represented using curly braces:

{
// code
}

Example:

{
const name = “Rahim”;

console.log(name);

}

let and const follow block scope.

LET EXAMPLE WITH BLOCK

let score = 10;

{
let score = 20;

console.log(score);

}

console.log(score);

Output:

20
10

The two score variables belong to different scopes.

CONST EXAMPLE WITH BLOCK

const language = “JavaScript”;

{
const language = “Python”;

console.log(language);

}

console.log(language);

Output:

Python
JavaScript

Again, these bindings exist in different scopes.

WHAT ABOUT VAR?

JavaScript also has an older variable declaration keyword:

var

Example:

var name = “Rahim”;

However, this class focuses on:

let

and:

const

Modern JavaScript commonly prefers let and const because their block-scoping behavior makes variable lifetimes easier to reason about.

You will focus primarily on let and const in this lesson.

LET AND CONST WITH DIFFERENT VALUES

Example:

const courseName =
“Web Development”;

let completedClasses =
41;

console.log(courseName);

console.log(completedClasses);

Now:

completedClasses = 42;

console.log(completedClasses);

Output:

Web Development
41
42

courseName remains unchanged.

completedClasses changes.

PRACTICAL STUDENT EXAMPLE

const studentName =
“Rahim”;

const courseName =
“JavaScript”;

let completedLessons =
1;

console.log(studentName);

console.log(courseName);

console.log(completedLessons);

completedLessons = 2;

console.log(completedLessons);

Output:

Rahim
JavaScript
1
2

PRACTICAL BANK BALANCE EXAMPLE

let balance = 5000;

const withdrawalAmount = 1000;

balance =
balance – withdrawalAmount;

console.log(balance);

Output:

4000

Why let for balance?

Because balance is reassigned.

Why const for withdrawalAmount?

Because that particular value is not reassigned in this example.

COMMON MISTAKES

  1. REASSIGNING CONST

Wrong:

const age = 20;

age = 21;

Use let if reassignment is required:

let age = 20;

age = 21;

  1. DECLARING CONST WITHOUT A VALUE

Wrong:

const name;

Correct:

const name = “Rahim”;

  1. STARTING VARIABLE NAME WITH A NUMBER

Wrong:

let 1name = “Rahim”;

Correct:

let name1 = “Rahim”;

  1. USING SPACES IN VARIABLE NAMES

Wrong:

let user name = “Rahim”;

Correct:

let userName = “Rahim”;

  1. USING A VARIABLE BEFORE UNDERSTANDING ITS SCOPE

Example:

{
const message = “Hello”;
}

console.log(message);

message is not available outside that block.

  1. CONFUSING REASSIGNMENT WITH REDECLARATION

Declaration:

let score = 10;

Reassignment:

score = 20;

Incorrect redeclaration in the same scope:

let score = 10;

let score = 20;

  1. THINKING CONST MAKES OBJECTS COMPLETELY IMMUTABLE

const prevents reassignment of the binding.

It does not automatically freeze objects or arrays.

  1. USING LET FOR EVERYTHING

This can work in many cases, but if a binding does not need reassignment, const communicates that intention more clearly.

  1. USING UNCLEAR NAMES

Avoid:

let x = 5000;

when:

const productPrice = 5000;

better communicates the purpose.

  1. CASE-SENSITIVITY MISTAKE

Example:

const userName = “Rahim”;

console.log(username);

userName and username are different identifiers.

NEW & DIFFICULT WORDS

Variable = Named reference used to work with a value in a program.

Declaration = Creating a variable binding.

Initialization = Providing an initial value when a variable is created.

Assignment = Giving a value to a variable.

Reassignment = Assigning a new value to an existing variable.

Binding = Association between an identifier and a value.

Identifier = Name used to identify something in code, such as a variable.

let = JavaScript keyword used to declare a block-scoped variable that can be reassigned.

const = JavaScript keyword used to declare a block-scoped binding that cannot be reassigned.

Block = Group of statements enclosed by curly braces.

Block Scope = Scope limited to the block where a binding is declared.

Scope = Region of code where a variable or identifier can be accessed.

camelCase = Naming convention such as userName or productPrice.

Case-Sensitive = Uppercase and lowercase letters are treated differently.

Immutable = Unable to be changed.

Object = JavaScript value containing properties.

Array = JavaScript structure commonly used to store ordered collections of values.

Keyword = Reserved word with a special meaning in a programming language.

IMPORTANT NOTES

The core concept of this class is:

let

Reassignable binding

const

Reassignment is not allowed

Example:

let score = 10;

score = 20;

Valid.

Example:

const score = 10;

score = 20;

Invalid.

Recommended basic approach:

Use const when reassignment is not needed.

Use let when reassignment is needed.

Remember:

const does NOT automatically make an object or array immutable.

Example:

const user = {
name: “Rahim”
};

This can be valid:

user.name = “Karim”;

But this is not:

user = {
name: “Karim”
};

PRACTICE

  1. Create a variable using let.
  2. Create a variable using const.
  3. Print both variables using console.log().
  4. Reassign the let variable.
  5. Try to reassign the const variable and inspect the error.
  6. Create:

const firstName = “Your Name”;

  1. Create:

let age = 20;

  1. Change age to 21.
  2. Create productName.
  3. Create productPrice.
  4. Create quantity.
  5. Calculate totalPrice.
  6. Create a stock variable and change it after a sale.
  7. Create two variables and combine their values.
  8. Practice camelCase variable names.
  9. Try an invalid variable name beginning with a number.
  10. Create a block using {}.
  11. Declare let inside the block.
  12. Try to access it outside the block.
  13. Create a const object and change one property.

ASSIGNMENT

Create a JavaScript file called:

variables.js

Create the following variables:

const studentName =
“Your Name”;

const courseName =
“Web Development”;

let completedClasses =
41;

const totalClasses =
120;

Print all values:

console.log(studentName);

console.log(courseName);

console.log(completedClasses);

console.log(totalClasses);

Then update:

completedClasses = 42;

Print the updated value:

console.log(completedClasses);

Next create a product example:

const productName =
“Keyboard”;

const productPrice =
1500;

let quantity =
2;

let totalPrice =
productPrice * quantity;

console.log(productName);

console.log(productPrice);

console.log(quantity);

console.log(totalPrice);

Now change:

quantity = 3;

Recalculate:

totalPrice =
productPrice * quantity;

console.log(quantity);

console.log(totalPrice);

Expected final quantity:

3

Expected final total:

4500

Finally:

  • Create one const variable.
  • Try to reassign it.
  • Check the console error.
  • Remove the incorrect reassignment.
  • Run the program again.

QUIZ

  1. What is a variable?

Answer:

A variable is a named reference used to work with a value in a program.

  1. Which two variable declaration keywords are the focus of this class?

Answer:

let and const.

  1. Can a let variable be reassigned?

Answer:

Yes.

  1. Can a const binding be reassigned?

Answer:

No.

  1. Which should generally be used when reassignment is not required?

Answer:

const.

  1. Which should be used when reassignment is required?

Answer:

let.

  1. Is this valid?

let score = 10;

score = 20;

Answer:

Yes.

  1. Is this valid?

const score = 10;

score = 20;

Answer:

No.

  1. Can const be declared without an initializer?

Answer:

No.

  1. Can let be declared before assigning a value?

Answer:

Yes.

  1. Are JavaScript variable names case-sensitive?

Answer:

Yes.

  1. Is this a valid variable name?

let userName;

Answer:

Yes.

  1. Is this valid?

let 1user;

Answer:

No.

  1. What naming convention is commonly used for JavaScript variables?

Answer:

camelCase.

  1. Are let and const block-scoped?

Answer:

Yes.

  1. Does const make an object completely immutable?

Answer:

No.

const prevents reassignment of the binding itself.

INTERVIEW QUESTIONS

Q: What is a variable in JavaScript?

Answer:

A variable provides a named binding that allows a program to work with a value.

Q: What is the difference between let and const?

Answer:

A let binding can be reassigned, while a const binding cannot be reassigned.

Q: When should you use const?

Answer:

Use const when the binding does not need to be reassigned.

Q: When should you use let?

Answer:

Use let when the variable needs to receive a different value later.

Q: Can you declare const without assigning an initial value?

Answer:

No. A const declaration requires an initializer.

Q: Are let and const block-scoped?

Answer:

Yes.

Q: What is reassignment?

Answer:

Reassignment means assigning a new value to an existing variable.

Q: What is the difference between declaration and reassignment?

Answer:

Declaration creates the variable binding.

Example:

let score = 10;

Reassignment gives that existing binding a new value.

Example:

score = 20;

Q: Does const make an object immutable?

Answer:

No.

It prevents the variable from being reassigned to another value, but the contents of an object may still be modified unless additional mechanisms are used.

Q: Why is const often used by default?

Answer:

When reassignment is unnecessary, const makes that intention explicit and prevents accidental reassignment.

CLASS SUMMARY

In Class 42, you learned JavaScript Variables: let and const.

You learned:

  • Variables
  • Variable declaration
  • Initialization
  • Assignment
  • Reassignment
  • let
  • const
  • let vs const
  • Default use of const
  • Variable naming
  • camelCase
  • Case sensitivity
  • Block scope
  • const with objects
  • const with arrays
  • Common variable mistakes
  • Practical variable examples

The most important rule:

const

Use when reassignment is not needed.

let

Use when reassignment is needed.

NEXT CLASS:

Class 043 — JavaScript Data Types

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

©2026 learnen.dinajpuritpark.com | Design: Newspaperly WordPress Theme