DEV Community

Cover image for JavaScript Variables: let vs const vs var
Ridoy Hasan
Ridoy Hasan

Posted on

JavaScript Variables: let vs const vs var

JavaScript Simplified: A Beginner's Guide to Mastering Interactive Web Development

Post 4 of 30: Understanding Variables in JavaScript

In this post, we’ll explore what variables are, how to declare them, and how they work in JavaScript.


What is a Variable?

A variable is a way to store a value in JavaScript so we can use it later. Think of a variable like a labeled box where you can keep things and retrieve them when needed.

For example, instead of writing "John" multiple times in your code, you can store it in a variable and use it anywhere.


Declaring Variables in JavaScript

JavaScript provides three ways to declare variables:

  • var (older method, not recommended)
  • let (modern and recommended for changeable values)
  • const (for values that should not change)

1. Using let (Recommended)

let name = "John";
console.log(name);
Enter fullscreen mode Exit fullscreen mode

Output:

John
Enter fullscreen mode Exit fullscreen mode

Here, we:

  • Created a variable called name
  • Assigned it the value "John"
  • Used console.log() to print the value of name

2. Using const (For Constant Values)

const PI = 3.1416;
console.log(PI);
Enter fullscreen mode Exit fullscreen mode

Output:

3.1416
Enter fullscreen mode Exit fullscreen mode
  • const is used for values that should not change.
  • Once assigned, you cannot reassign a new value to PI.

3. Using var (Older Method – Avoid Using)

var age = 25;
console.log(age);
Enter fullscreen mode Exit fullscreen mode

Output:

25
Enter fullscreen mode Exit fullscreen mode
  • var was commonly used before let and const, but it has scoping issues, so use let or const instead.

Changing Variable Values

With let, you can change a variable’s value, but with const, you cannot.

Example with let:

let city = "New York";
console.log(city); // Output: New York

city = "Los Angeles"; // Changing the value
console.log(city); // Output: Los Angeles
Enter fullscreen mode Exit fullscreen mode

Example with const (This will cause an error)

const country = "USA";
console.log(country);

country = "Canada"; // ❌ This will cause an error
console.log(country);
Enter fullscreen mode Exit fullscreen mode

Error: Uncaught TypeError: Assignment to constant variable.


Variable Naming Rules

When naming variables, follow these rules:

✔️ Can contain letters, numbers, $, and _

✔️ Must start with a letter, $, or _ (not a number)

✔️ Case-sensitive (name and Name are different)

✔️ Cannot be a reserved keyword (like let, console, function, etc.)

Examples of Valid Variable Names:

let firstName = "Alice";
let _score = 90;
let $price = 20;
let userAge = 25;
Enter fullscreen mode Exit fullscreen mode

Examples of Invalid Variable Names:

let 1name = "Bob";  // ❌ Cannot start with a number
let let = 50;       // ❌ "let" is a reserved keyword
let user-age = 30;  // ❌ Hyphens are not allowed
Enter fullscreen mode Exit fullscreen mode

Practical Exercise: Storing and Changing Values

Try this in your script.js file:

let favoriteFood = "Pizza";
console.log("My favorite food is:", favoriteFood);

favoriteFood = "Burger";
console.log("Now, my favorite food is:", favoriteFood);
Enter fullscreen mode Exit fullscreen mode

Expected Output:

My favorite food is: Pizza
Now, my favorite food is: Burger
Enter fullscreen mode Exit fullscreen mode

Next Steps

Now that we understand how variables work, the next step is to explore data types in JavaScript—including numbers, strings, booleans, and more!

Stay tuned for the next post! 🚀


Pro Tip:

🔹 Use let when you expect the value to change.

🔹 Use const when the value should stay the same.

🔹 Avoid var unless you specifically need it.

Follow me on LinkedIn - Ridoy Hasan
Visit my website - Ridoyweb
*Visit my agency website *- webention digital

Top comments (1)

Collapse
 
sudo_091 profile image
TechTales.io

Great content

Some comments may only be visible to logged-in visitors. Sign in to view all comments.