JavaScript · Lesson 06 / 32 · English
JavaScript Variables
Understand the idea. Change the code. Build confidence with one focused experiment.
Read this lesson in தமிழ் ↗.
Start with the idea
Use const when the binding will not be reassigned and let when it will. A const object can still have its properties changed; const protects the binding, not every value inside it.
Learning goal: Explain what the example does, predict its result, and change it to solve the practice task.
Step-by-step lesson notes
Variables in JavaScript
Variables are containers for storing data values. In modern JavaScript (ES6+), we have three ways to declare variables: var, let, and const.
var, let, and const
| Keyword | Scope | Reassignable | Hoisted | Use Case |
|---|---|---|---|---|
var | Function | Yes | Yes (undefined) | Avoid — legacy code only |
let | Block | Yes | No (TDZ) | Variables that change |
const | Block | No | No (TDZ) | Values that don't change |
// var — function-scoped, avoid in modern code
var name = "Alice";
// let — block-scoped, can be reassigned
let score = 100;
score = 200; // ✅ Allowed
// const — block-scoped, cannot be reassigned
const PI = 3.14159;
// PI = 3; // ❌ TypeError: Assignment to constant variableData Types
JavaScript is dynamically typed — variables can hold any type of data, and the type can change at runtime.
// Primitive Types
let text = "Hello"; // String
let age = 25; // Number
let price = 19.99; // Number (no separate float)
let isActive = true; // Boolean
let nothing = null; // Null
let notDefined; // Undefined
let id = Symbol("id"); // Symbol (ES6)
let bigNum = 9007199254740991n; // BigInt (ES2020)
// Reference Types
let arr = [1, 2, 3]; // Array
let obj = { name: "Bob" }; // Object
let fn = () => {}; // Function
console.log(typeof text); // "string"
console.log(typeof age); // "number"
console.log(typeof isActive); // "boolean"
console.log(typeof nothing); // "object" (historical quirk!)
console.log(typeof notDefined); // "undefined"Quirk: typeof null returns "object" — this is a long-standing bug in JavaScript kept for backward compatibility.
Variable Naming Rules
- Must start with a letter, underscore (_), or dollar sign ($)
- Can contain letters, digits, underscores, and dollar signs
- Case-sensitive (myVar and myvar are different)
- Cannot use reserved keywords (let, const, return, etc.)
- Use camelCase by convention: myVariableName
let firstName = "Alice"; // ✅ camelCase (preferred)
let _private = 42; // ✅ underscore prefix
let $price = 9.99; // ✅ dollar sign
// let 2fast = true; // ❌ starts with digit
// let my-name = "Bob"; // ❌ hyphens not allowedA complete example
Run this complete index.js program in the JavaScript console editor below. The earlier reference fragments may need a browser page or other surrounding setup.
const learner = "Kavi";
let completed = 1;
completed = completed + 2;
console.log(learner + ": " + completed);Predict first. Reveal the expected result.
Kavi: 3
Make one change. See what happens.
Load the example, change a value, predict the output, and run it again. Use the console output to check your reasoning.
Wait for the editor to load, then load this example and press Run.
Powered by the free OneCompiler editor. Code you run is sent to that external service. Keep personal data and secret keys out of your examples.
Put the idea to work
Practice challenge
Start completed at 4, then add 2.
Reveal the answer and reasoning
The output is Kavi: 6. Keep completed as let because its binding changes.
One mistake to avoid: Assigning a new value to a const binding throws a TypeError. Use let for a changing counter.
Your learning checkpoint
- Explain the example in your own words.
- Complete the challenge before revealing its answer.
- Describe the common mistake and how to avoid it.