Variables and Data Types in JavaScript: A Beginner's Guide

Think of a variable as a box. You write a label on it, put something inside, and refer to it by that label whenever you need it. That's what variables do in JavaScript. They hold information your program can use, change, or pass around.
Without variables, you'd have to repeat the same values everywhere. Want to show a user's name in three different places? You'd hard-code it three times. Change it once, and you've broken the other two. Variables solve that.
Declaring Variables
JavaScript gives you three ways to create a variable: var, let, and const.
var is the original way, around since JavaScript's early days:
var name = "Alice";
let was introduced later and is now the standard choice for variables whose values might change:
let age = 25;
const is for values that won't change after you set them:
const country = "India";
You'll mostly use let and const in modern code. var still works, but it has some quirky behavior that trips up beginners, so there's little reason to reach for it.
Primitive Data Types
Every value you store has a type. JavaScript has five primitive types worth knowing as a beginner.
String
A string is text. Wrap it in single or double quotes (both work):
let firstName = "Riya";
let greeting = 'Hello, world!';
Number
Numbers don't need quotes, whether whole or decimal:
let age = 22;
let price = 199.99;
Boolean
A boolean is either true or false. Nothing else:
let isLoggedIn = true;
let hasSubscription = false;
These are useful when your code needs to make a decision. Is the user logged in? Show the dashboard. If not, show the login page.
Null
null means "intentionally empty." You set a variable to null when you want to say it exists, but has no value right now:
let selectedItem = null;
Undefined
undefined means a variable was declared but never given a value:
let score;
console.log(score); // undefined
The difference is subtle but real. null is an intentional absence. undefined is just... nothing was set yet.
How Values Can Change
With let, you can update a variable after it's been set:
let mood = "tired";
mood = "awake";
console.log(mood); // "awake"
With const, that's not allowed. Try it and JavaScript will throw an error:
const city = "Mumbai";
city = "Delhi"; // Error: Assignment to constant variable.
This is actually useful. When you use const, you're making a small promise to yourself and anyone reading your code: this value won't change. That makes code easier to follow.
A Quick Word on Scope
Scope is the question "where in my code can I actually use this variable?"
Imagine the box analogy again. If you put a box inside a room, you can use it inside that room. But you can't take it outside and expect it to still be there.
function greetUser() {
let message = "Hello!";
console.log(message); // works fine
}
console.log(message); // Error: message is not defined
message was declared inside the function. Outside of it, JavaScript has no idea it exists.
This is called block scope, and it's how let and const work. var behaves differently here, which is one reason it's fallen out of favor. With var, values can "leak" outside the block they were declared in, and that causes bugs that are genuinely annoying to track down.
Putting It Together
Here's a simple example using everything covered:
const userName = "Arjun";
let userAge = 20;
let isPremiumMember = false;
console.log(userName); // "Arjun"
console.log(userAge); // 20
console.log(isPremiumMember); // false
userAge = 21; // birthday
console.log(userAge); // 21
userName uses const because names don't change. userAge uses let because it will. isPremiumMember is a boolean, sitting at false until the user upgrades.
Conclusion
Variables store values your code can use later
Use
constby default; switch toletif the value needs to change; avoidvarStrings hold text, numbers hold numeric values, booleans hold true/false
nullis an empty value you set on purpose;undefinedmeans you just didn't set anything yetScope controls where a variable is visible in your code
These concepts show up constantly in JavaScript. Once they click, the rest of the language gets a lot easier to follow.



