Printing and PDF export are disabled for this content. View it online at Full Stack Learning Simplified.
Running JavaScriptπ± Beginner
In JavaScript, Variables act as labeled storage containers for data. You declare a variable, store a piece of information inside it, and reuse it throughout your application.
What are let and const?
Modern JavaScript (ES6+) relies on two primary keywords to declare variables:
| Keyword | Behavior | When to use it |
|---|---|---|
| const | Constant. Cannot be reassigned. | Default choice. Use this 90% of the time to prevent bugs. |
| let | Mutable. Can be reassigned later. | Use only when you know the value will change (like a counter in a loop). |
| var | Legacy mutable variable. | NEVER USE THIS. It has confusing scoping rules. |
Why Does const Matter?
Using const protects your code. If you define const taxRate = 0.05, and later in the code accidentally try to overwrite it with taxRate = 0.99, JavaScript will throw an error and crash, saving you from a massive financial bug.
How to Use Variables
You declare the keyword, the variable name, and assign a value.
script.js
// A constant that will never change
const username = "Alice";
// A mutable variable that we will change
let score = 0;
// Reassigning 'let' works perfectly
score = 10;
score = score + 5;
console.log(username + " has a score of " + score); // "Alice has a score of 15"Critical Warning: If you use
const for an Array or an Object, you cannot reassign the variable to a new object, but you CAN still modify the internal contents (like pushing to the array). const only protects the exact memory reference, not the deep contents!Free preview. Sign in and subscribe to unlock all 982 lessons across 31 courses.
Free preview Β· Β© 2026 Full Stack Learning Simplified