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:
1. Using let (Recommended)
let name = "John";
console.log(name);
Output:
John
Here, we:
2. Using const (For Constant Values)
const PI = 3.1416;
console.log(PI);
Output:
3.1416
3. Using var (Older Method – Avoid Using)
var age = 25;
console.log(age);
Output:
25
Changing Variable Values
With let, you can change a variable’s value, but with const, you cannot.
Recommended by LinkedIn
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 en Starbucks Cambodia/Laos
4moHola
Founder & CTO @Webention | Scaling International Brands and Businesses Through AI-driven Branding and Marketing Solutions
4moMore tip: Always use const when possible! It prevents accidental changes and makes your code more reliable.
Founder & CTO @Webention | Scaling International Brands and Businesses Through AI-driven Branding and Marketing Solutions
4moAs we know Variables store data for later use and Understanding them is the first step in mastering JavaScript!
Founder & CTO @Webention | Scaling International Brands and Businesses Through AI-driven Branding and Marketing Solutions
4moTip: Use const when values shouldn’t change and let for flexibility. 💡