Close Menu
Developers GroundDevelopers Ground
  • Java
  • JavaScript
  • NodeJS

Subscribe to Updates

Get the latest creative news from FooBar about art, design and business.

What's Hot

Get Started With Node.js: A Beginner’s Guide to Your First Application

August 13, 2026

Node.js for Beginners: What Is Node.js and How Does It Work?

August 13, 2026

Java Variables and Data Types: A Beginner’s Guide

August 13, 2026
Facebook Instagram
Developers GroundDevelopers Ground
  • Java
  • JavaScript
  • NodeJS
Facebook Instagram
Developers GroundDevelopers Ground
Home»JavaScript»JavaScript Variables Explained: let, const and var for Beginners
JavaScript

JavaScript Variables Explained: let, const and var for Beginners

Omkar PanditBy Omkar PanditAugust 13, 20264 Mins Read
Facebook Twitter LinkedIn WhatsApp Email
Share
Facebook Twitter LinkedIn Email WhatsApp

JavaScript Variables Explained: let, const and var for Beginners

Variables are a fundamental part of almost every programming language. In JavaScript, variables allow developers to store information that can be used later in a program.

For example, an application may need to store a user’s name, age, product price, or login status.

JavaScript provides three commonly known variable declarations:

  • let
  • const
  • var

Although all three can be used to create variables, they behave differently.

What Is a Variable?

A variable is a named location used to store a value.

For example:

let username = "Rahul";
 

In this example, username is the variable and "Rahul" is its value.

The value can then be used elsewhere:

console.log(username);
 

The output will be:

Rahul

Using let in JavaScript

The let keyword is used when you expect the value of a variable to change.

For example:

let score = 10;

score = 20;

console.log(score);
 

The value starts as 10 and is later changed to 20.

This makes let useful for values that need to be updated during program execution.

For example:

let counter = 0;

counter = counter + 1;
counter = counter + 1;

console.log(counter);
 

The final value is 2.

Using const in JavaScript

The const keyword is used when a variable should not be reassigned after it has been created.

For example:

const websiteName = "Developers Ground";
 

You cannot later do this:

websiteName = "Another Website";
 

JavaScript will generate an error because a const variable cannot be reassigned.

For values that are not expected to change, const is generally a good choice.

Using var in JavaScript

var is the older way of declaring variables in JavaScript.

Example:

var city = "Pune";

console.log(city);
 

Modern JavaScript code generally prefers let and const because they provide clearer scoping behavior.

You may still encounter var when working with older JavaScript projects or legacy code.

Difference Between let, const and var

The most important differences can be summarized like this:

Keyword Can be reassigned? Scope
let Yes Block scope
const No Block scope
var Yes Function scope

For most modern JavaScript development, you will frequently work with let and const.

What Is Block Scope?

Block scope means that a variable exists only inside the block where it was declared.

For example:

if (true) {
    let message = "Hello";
    console.log(message);
}
 

The variable message exists inside the if block.

Trying to access it outside the block will not work:

if (true) {
    let message = "Hello";
}

console.log(message);
 

This produces an error because message is not available outside its block.

Why Is const Commonly Preferred?

Developers often prefer const when they know that a variable does not need to be reassigned.

For example:

const firstName = "Amit";
const age = 25;
const country = "India";
 

Using const makes your intention clearer.

If you later realize that a value needs to change, you can use let.

For example:

let currentPage = 1;

currentPage = 2;
 

Can Objects Declared With const Change?

This is an important JavaScript concept.

Consider:

const user = {
    name: "Amit",
    age: 25
};
 

You cannot reassign the entire variable:

user = {};
 

However, you can modify properties inside the object:

user.age = 26;
 

This works because const prevents reassignment of the variable itself. It does not automatically make the object immutable.

Can Arrays Declared With const Change?

The same idea applies to arrays.

const languages = ["JavaScript", "Java"];
 

You cannot replace the entire array:

languages = ["Python"];
 

But you can modify its contents:

languages.push("PHP");
 

Which One Should Beginners Use?

A simple rule for modern JavaScript is:

Use const by default. Use let when the value needs to change.

Use var mainly when you are working with older code that already uses it.

For example:

const website = "Developers Ground";

let visitors = 100;

visitors = visitors + 10;
 

Here, website remains unchanged, while visitors can be updated.

Final Thoughts

Understanding variables is an important first step toward learning JavaScript.

Remember these three points:

  • const is useful when a variable should not be reassigned.
  • let is useful when a value needs to change.
  • var is an older declaration style that you may encounter in existing projects.

Once you understand variables, you can move on to other JavaScript fundamentals such as functions, arrays, objects, conditions, loops, and asynchronous programming.

Share. Facebook Twitter LinkedIn WhatsApp Email
Omkar Pandit
  • Website

Related Posts

JavaScript August 13, 2026

JavaScript for Beginners: What Is JavaScript and How Does It Work?

Leave A Reply Cancel Reply

Don't Miss
NodeJS August 13, 2026

Get Started With Node.js: A Beginner’s Guide to Your First Application

Ready to write your first Node.js application? Follow this beginner-friendly guide to set up a Node.js project, understand package.json, and create a simple application.

Node.js for Beginners: What Is Node.js and How Does It Work?

August 13, 2026

Java Variables and Data Types: A Beginner’s Guide

August 13, 2026

Java for Beginners: What Is Java and How Does It Work?

August 13, 2026
Facebook Instagram
  • Our Authors
  • Get In Touch
  • Privacy Policy
© 2026 Developers Ground

Type above and press Enter to search. Press Esc to cancel.