Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Variables, data types, and scope explained simply.

Updated
6 min read
Understanding Variables and Data Types in JavaScript

Hey everyone! I have been diving deep into JavaScript recently as part of Web Dev Cohort 2026, and I realized that while we all rush to build fancy projects, sometimes our basics are a bit shaky. So, I decided to write this blog to really understand and strengthen the concepts of Variables and Data Types.

If you are an absolute beginner or just refreshing your memory, this is for you. Let's break it down simply.

What Exactly is a Variable?

Okay, before we write any code, let's use a real-life analogy because that is the easiest way to remember things.

Imagine you are moving houses. You have a bunch of cardboard boxes.

  • You put your books in one box.

  • You put your clothes in another.

  • You put your kitchen stuff in a third.

To know what is inside without opening them, you grab a marker and write a label on the side: Books , Clothes , Kitchen.

In JavaScript:

  • The Box is the memory space.

  • The Label is the Variable Name.

  • The Stuff inside is the Value.

That's it. A variable is just a named container (storage location) for holding data. We need them because programs deal with data, names, scores, prices and we need a way to find and use that data later.

How to Create Variables: var, let, and const

In the old days, JavaScript had just one way to do this. Now, we have three. This can be confusing at first, but its actually pretty simple.

1. var ( The Old School Way)

This is how we used to write JavaScript back in the day. It works, but it has some quirks that can cause bugs (more on scope later).

var myName = "Purakhnath"
console.log(myName); // Output: Purakhnath

2. let: The Modern Standard

let is the modern way to declare variables that you plan to change later. If your value needs to be updated (like a score in a game), use let.

let score = 29;
score = 63; // This is allowed
console.log(score); // Output: 63

3. const: The Constant

const is short for constant. Use this for variables that should never change. Think of settings or configuration values. Once you put something in this box, it stays there.

const birthYear = 2000;
birthYear = 2001; // Type Error: Assignment to constant variable.

Primitive Data Types: What Goes in the Box?

Okay, so we know how to make the boxes. But what kind of stuff can we actually put inside them? In JavaScript, the simple data types are called Primitives.

Here are the 5 main ones you will use 99% of the time

1. String

Think of a string as text. It is just a sequence of characters wrapped in quotes. You can use single quotes ('') or double quotes ("").

let userName = "Purakhnath"; // String
let message = 'Hanji'; // Also a string

2. Number

JavaScript doesn't care if it's a whole number or a decimal. They are all just numbers.

let userScore = 25;       // Number (Integer)
let price = 99.99;      // Number (Float/Decimal)

3. Boolean

This is the simplest one. It is just true or false. We use this for logic (like checking if a user is logged in).

let isLoggedIn = true
let hasPaid = false

4. Undefined

This happens when you create a box (variable) but forget to put anything in it. The variable has been declared but not assigned a value.

let favoriteFood
console.log(favoriteFood) // Output: undefined

5. Null

This is a little different from undefined. Null represents an intentional absence of value. It means the developer purposely said -> This box has nothing in it.

let currentJob = null

The Difference Between var, let, and const

Here is the simple breakdown

Keyword

Can change value?

Scope

Use Case

var

Yes

Function Scope

Avoid using it. It's outdated.

let

Yes

Block Scope

Use for values that change (counters, scores).

const

No (cannot be reassigned)

Block Scope

Use by default for everything else.

When in doubt, always start with const. If you realize you need to update the value later, change it to let. Avoid var in modern code.

What is Scope ?

You might have seen the word Scope and got scared. Don't be. Scope simply means where a variable is accessible in your code.

Imagine you are in your house.

  • Global Scope: Variables declared outside any curly braces {}. These are like the living room -> everyone in the house can see them and use them.

  • Block Scope: Variables declared inside curly braces {} (like inside an if statement). These are like a bedroom -> only people inside that room can see them.

This is why let and const are safer. They stick to their room (Block Scope).

if (true) {
    let blockVar = "I am inside the block"
    console.log(blockVar) // Works fine -> I am inside the block
}

console.log(blockVar) // Reference Error: blockVar is not defined

Function Scope (var)

var is older and behaves differently. It ignores blocks and stays inside the function (or becomes global if not in a function). This can lead to bugs where variables leak out where they should not be.

if (true) {
    var leakyVar = "I leaked out"
}

console.log(leakyVar); // Works! (But this is usually not what we want)

Practical Assignment: Try This Yourself

Okay, reading is great, but coding is better. I wrote this small snippet to test these concepts. You should try running this in your browser console or a code editor like VS Code.

const fullName = "Purakhnath Jyani"
let age = 21;                      
const isStudent = true;            

console.log("Name:", fullName)
console.log("Age:", age)
console.log("Is Student:", isStudent)

age = 26;  // This works
console.log("Updated Age:", age)

fullName = "Hitesh Choudhary"; // This will give an error

Summary

Writing this out really helped me solidify the concepts. Here is the cheat sheet

  1. Variables are containers (boxes) for data.

  2. Data Types tell the computer what kind of data is in the box (String, Number, Boolean, etc.).

  3. Use const by default, let if you need to change the value later, and avoid var.

  4. Scope is just where can I access this variable?

Thanks for reading. See you in the next blog.

More from this blog