Article 2 of 30 -JavaScript Variables: let vs const vs var
Photo credit @Freepik

Article 2 of 30 -JavaScript Variables: let vs const vs var

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

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);        

Output:

John        

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);        

Output:

3.1416
        

  • 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);        

Output:

25
        

  • 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
        

Example with const (This will cause an error)

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

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

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;
        

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
        

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);
        

Expected Output:

My favorite food is: Pizza
Now, my favorite food is: Burger
        

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.

Read the first Article -


Anghela Anette

Anghela en Starbucks Cambodia/Laos

4mo

Hola

Like
Reply
Ridoy Hasan

Founder & CTO @Webention | Scaling International Brands and Businesses Through AI-driven Branding and Marketing Solutions

4mo

More tip: Always use const when possible! It prevents accidental changes and makes your code more reliable.

Like
Reply
Ridoy Hasan

Founder & CTO @Webention | Scaling International Brands and Businesses Through AI-driven Branding and Marketing Solutions

4mo

As we know Variables store data for later use and Understanding them is the first step in mastering JavaScript!

Like
Reply
Ridoy Hasan

Founder & CTO @Webention | Scaling International Brands and Businesses Through AI-driven Branding and Marketing Solutions

4mo

Tip: Use const when values shouldn’t change and let for flexibility. 💡

Like
Reply

To view or add a comment, sign in

More articles by Ridoy Hasan

  • What is JavaScript? and Why You Should Learn It?

    Introduction Welcome to the first post of our JavaScript series! If you're new to coding or web development, you've…

    2 Comments
  • CSS Display – Controlling the Layout Behavior of Elements

    In this article, we’ll discuss one of the most important CSS properties: . The property controls the layout behavior of…

    1 Comment
  • Unlocking the Power of CSS Position: A Complete Guide with Examples

    When building a website, the layout is crucial to the overall user experience. CSS (Cascading Style Sheets) helps you…

    2 Comments
  • Mastering CSS Grid for Advanced Layouts

    What is CSS Grid? CSS Grid is a layout system that enables you to design advanced layouts by defining rows and columns,…

  • Mastering CSS Flexbox

    In this lecture, we’ll dive deeper into CSS Flexbox, a powerful layout tool that helps you design responsive and…

    3 Comments
  • Typography and Font Styling in CSS

    Typography plays a crucial role in the aesthetics and readability of a website. Good typography not only enhances user…

    5 Comments
  • Colors and Backgrounds in CSS

    Lecture 3: Colors and Backgrounds in CSS In this lecture, we'll explore how to use colors and backgrounds to make your…

    1 Comment
  • CSS Box Model In Depth

    Lecture 3: Understanding the CSS Box Model In this lecture, we’ll explore one of the most fundamental concepts in CSS:…

    2 Comments
  • CSS: selectors and properties

    Lecture 2: Selectors and Properties In this lecture, we'll dive into the building blocks of CSS: selectors and…

  • Lecture 1: Introduction to CSS

    Welcome to the first lecture of "Basic to Brilliance" - your journey to mastering CSS starts here! What is CSS? CSS, or…

    4 Comments

Insights from the community

Others also viewed

Explore topics