JavaScript Variables 101 - Understanding and Using Variables Effectively

Srijan } Author Pic
SrijanJan 24, 2025 - JavaScript
JavaScript Variables 101 - Understanding and Using Variables Effectively - Reacted Node

Photo: Andres Ayrton/Pexels

Introduction

If you are starting your journey into the exciting world of JavaScript, welcome! You are about to meet one of the most essential tools in your coding toolkit: Variables.

What is a Variable?

Imagine you have a special box with a label on it. You can put all sorts of things inside – a toy car, a handful of candy, or even a secret message. You can change what's inside the box, but the label stays the same.

In JavaScript, a variable is like that labeled box. It's a way to store a piece of information – whether it's a number (like your age), a word (like your name), or even something more complex (like a list of your favorite foods). You give each variable a unique name (the label on the box) to easily find and use that information whenever you need it in your code.

What's the Difference Between a Value and a Variable?

Let's make the difference between a value and a variable super clear:

  • Value: Value is the actual data or information itself. Think of it as the contents of your box. It could be 42, "hello," or a more elaborate structure like a list of your favorite foods.
  • Variable: Variable is the name you give to that value, the label on your box. It's how you refer to and access the value stored in memory.
1let age = 25; // "age" is the variable, 25 is the value
2let message = "welcome";
3// "message" is the variable, "welcome is the value

Why Use Variables?

Variables are your trusty companions on your coding quest for several important reasons:

Reusability

Imagine you have a favorite recipe for chocolate chip cookies. You wouldn't want to write down the amount of flour, sugar, and butter only a few times you bake them, right? Instead, you should jot it down once on an index card and refer to it whenever needed.

Example You might store a tax rate in a variable and use it multiple times to calculate prices for different items.

1// 8% tax rate
2let taxRate = 0.08;
3
4let shirtPrice = 10;
5
6// Applying tax to shirtPrice
7let shirtPriceWithTax = shirtPrice + shirtPrice * taxRate;
8
9let socksPrice = 25;
10
11// Reusing taxRate for socksPrice
12let socksPriceWithTax = socksPrice + socksPrice * taxRate;

Readability

Imagine following a recipe with the ingredients listed as "Ingredient A," "Ingredient B," and so on. It would be confusing. You still need to learn what you were putting in the bowl! Variables with descriptive names make your code much easier to understand.

Example Let's say you're calculating the total cost of an online order.

Without variables

1let totalCost = 29.99 + 14.99 + 5.99 + (29.99 + 14.99 + 5.99) * 0.08;

With variables

1let shirtPrice = 29.99;
2let pantsPrice = 14.99;
3let socksPrice = 5.99;
4let taxRate = 0.08;
5
6let subtotal = shirtPrice + pantsPrice + socksPrice;
7let totalCost = subtotal + subtotal * taxRate;

The second example is much more precise. We can immediately understand what each value represents and how each item contributes to the final calculation.

Adaptability

Think of variables like containers with different locking mechanisms. Some (declared with let or var) have open lids, so you can easily swap out the contents for something completely different. Others (declared with const) have a latch that prevents you from replacing the entire container with a new one. Still, if the container holds smaller compartments (like an object with properties or an array with elements), you can open those compartments and change the items inside.

1const shoppingCart = { apples: 3, oranges: 2 };
2shoppingCart.apples = 5; // You can change the number of apples
3shoppingCart = { bananas: 4 }; // But you can't replace the whole cart

A Coder's Confession

When I first started coding, I tried to cram everything into one giant line of code, like stuffing too many items into a backpack. It was a disaster! However, once I started using variables, my code became much more organized and easier to work with. It was like finally finding that enchanted bag of holding!

So, there you have it – variables are your trusty inventory slots in JavaScript. Get to know them well, and they'll help you conquer any coding challenge that comes your way!