Variables, Data Types, and Scope in JavaScript

Every program needs to remember things — a user's name, their age, whether they are logged in or not. Variables are how JavaScript stores and manages that information. If you are just starting out, this is one of the first concepts you need to get comfortable with.
What Is a Variable?
Think of a variable as a labeled box. You give the box a name, put something inside it, and refer to it by that name whenever you need what is stored inside.
Box Label: name
Box Contents: "Alice"
In JavaScript, that looks like this:
let name = "Alice";
You just created a box called name and put the value "Alice" inside it. Whenever you need the name, you just use name.
Declaring Variables: var, let, and const
JavaScript gives you three keywords to declare variables.
let
Use let when the value might change later.
let age = 25;
age = 26; // perfectly fine
console.log(age); // 26
const
Use const when the value should never change after it is set.
const country = "India";
country = "USA"; // TypeError: Assignment to constant variable
Once assigned, a const variable cannot be reassigned. Trying to do so throws an error.
var
var is the old way of declaring variables, used before let and const were introduced. You will still see it in older code, but modern JavaScript favors let and const.
var city = "Mumbai";
city = "Delhi"; // allowed
Comparison at a glance
| Keyword | Reassign? | Scope | When to Use |
|---|---|---|---|
| var | Yes | Function | Legacy code only |
| let | Yes | Block | Values that change |
| const | No | Block | Values that are fixed |
Primitive Data Types
Every value you store in a variable has a type. JavaScript has five primitive data types you will use constantly as a beginner.
String
A string is text. Wrap it in single quotes, double quotes, or backticks.
let name = "Alice";
let message = 'Hello, world!';
let note = `This is a template string`;
Number
A number is any numeric value — integer or decimal.
let age = 28;
let price = 99.99;
let rating = 4.5;
Boolean
A boolean is either true or false. Nothing else.
let isLoggedIn = true;
let isStudent = false;
Null
null means a variable intentionally has no value. You set it on purpose.
let selectedItem = null; // nothing selected yet
Undefined
undefined means a variable has been declared but not given a value yet.
let score;
console.log(score); // undefined
Quick reference
| Type | Example |
|---|---|
| String | "Alice", 'hello', world |
| Number | 42, 3.14, -7 |
| Boolean | true, false |
| Null | null |
| Undefined | declared but not assigned |
What Is Scope?
Scope determines where in your code a variable can be accessed. Think of it like rooms in a house. A variable declared inside a room is only available in that room — not in other rooms, and not outside the house.
Block Scope (let and const)
A block is any code wrapped in curly braces {}. Variables declared with let or const only exist inside the block they are defined in.
{
let message = "Hello";
console.log(message); // "Hello" — works fine inside the block
}
console.log(message); // ReferenceError — not accessible outside
Function Scope (var)
Variables declared with var are scoped to the entire function they are in — not just the block.
function greet() {
var name = "Alice";
console.log(name); // "Alice"
}
console.log(name); // ReferenceError — outside the function
Simple scope visualization
Global Scope
|
|-- function greet() {
| var name = "Alice" <- accessible only inside greet()
|
| if (true) {
| let city = "Mumbai" <- accessible only inside this block
| const age = 25 <- accessible only inside this block
| }
| }
A variable at the outer level can be accessed by inner code. But a variable declared inside a block or function cannot be seen from outside.
Putting It All Together
const username = "Raj"; // will never change
let score = 0; // will change as user plays
let isActive = true; // boolean flag
let lastLogin = null; // no login recorded yet
let streak; // declared, not assigned yet
console.log(username); // "Raj"
console.log(score); // 0
console.log(isActive); // true
console.log(lastLogin); // null
console.log(streak); // undefined
score = 10;
console.log(score); // 10
Practice Assignment
Work through these steps to practice what you have learned:
1. Declare variables for name, age, and isStudent:
let name = "Raj";
let age = 21;
const isStudent = true;
2. Print them in the console:
console.log(name); // "Raj"
console.log(age); // 21
console.log(isStudent); // true
3. Try changing the values and observe behavior:
// let — can be changed
age = 22;
console.log(age); // 22
// const — cannot be changed
isStudent = false; // TypeError: Assignment to constant variable
Run this in your browser console. The error on the last line is expected — that is exactly how const is supposed to behave.
Quick Recap
A variable is a named container that stores a value
Use
letfor values that will change,constfor values that will notAvoid
varin modern code — preferletandconstJavaScript has five primitive types: string, number, boolean, null, and undefined
nullis intentionally empty;undefinedmeans a variable was never assigned a valueScope controls where a variable can be accessed —
letandconstare block-scoped,varis function-scoped
Getting these fundamentals right sets a solid foundation for everything else in JavaScript. Every concept you learn from here — functions, objects, classes, arrays — builds directly on top of this.
Happy coding! 🚀
If you enjoyed this article, check out my other blogs on this profile. 🔗 Connect with me: LinkedIn | GitHub | X (Twitter)




