Skip to main content

Command Palette

Search for a command to run...

Variables and Data Types in JavaScript

Updated
6 min readView as Markdown
Variables and Data Types in JavaScript

When someone starts learning JavaScript, the very first thing that usually feels confusing is this idea of variables. At first, the word sounds technical, almost like something difficult, but in reality, it is one of the most human ideas in programming.

Imagine you are writing something important on paper. Instead of writing the same value again and again, you give it a name so you can refer to it easily later. Programming works in a very similar way.

Suppose you want to store your name inside a program. Instead of repeatedly typing "Rahul" every time, you can simply place it inside a variable:

let name = "Rahul";

This line may look very small, but it teaches an important idea. The word let tells JavaScript that you are creating a variable. The word name becomes the label, and "Rahul" is the value stored inside it.

A simple way to think about this is to imagine a container sitting on a table . You stick a label on it that says name, and inside that container you place the value Rahul. Later, whenever you need that value, you simply call the label instead of writing the value again.

If you write:

console.log(name);

JavaScript looks inside that container and prints the stored value.

This is why variables matter so much. They help programs remember information.

Now imagine you are building something slightly more useful. Suppose you are writing a small shopping calculation. A product costs 500, tax is 50, and you want total price.

let price = 500;
let tax = 50;
let total = price + tax;

console.log(total);

Here something interesting happens. JavaScript is not just storing values anymore. It is using stored values to create a new result.

The variable price remembers 500.

The variable tax remembers 50.

Then total combines both.

This is why variables are often called the memory of a program .

Without variables, code becomes repetitive and hard to manage. Imagine if every time you wanted total price, you had to rewrite every number manually. Small programs may survive that, but larger programs quickly become messy.

Different ways to create variables

As beginners continue learning, they notice that JavaScript offers three different ways to create variables:

var city = "Kolkata";
let age = 21;
const country = "India";

At first this creates another question: why three ways?

The answer is history and control.

var

var is the older style. In early JavaScript, almost everyone used it. But over time developers discovered that var behaves in ways that sometimes create confusion, especially inside blocks and functions.

Because of that, modern JavaScript introduced let and const.

let

When you use let, you are saying: this value may change later.

let score = 10;
score = 20;

JavaScript accepts this because let allows reassignment.

This is useful when values naturally change, like game scores, counters, temperatures, or user input.

const

But sometimes you know a value should remain fixed. For example:

const pi = 3.14;

The value of pi should not change during the program. So const protects it.

If someone later writes:

pi = 5;

JavaScript stops and throws an error because the program is trying to break a constant rule.

This small difference teaches an important mindset: some information changes, some information should stay stable .

Datatypes

Now variables alone are not enough. JavaScript also needs to understand what kind of value you are storing. This is where data types enter.

A variable can hold text, numbers, true/false values, empty values, and more.

Strings

For example, if you write:

let language = "JavaScript";

This is text. In programming, text is called a string.

Anything inside quotes becomes a string. It does not matter whether the text contains one word, one letter, or a full sentence.

let message = "Learning code is fun";

JavaScript sees that and understands: this is text, not a number.

That matters because text behaves differently from numbers.

For example:

let a = "5";
let b = 2;
console.log(a + b);

The result becomes:

52

Not 7.

Why? Because "5" is text, so JavaScript joins them instead of adding mathematically.

That is the first moment many beginners realize data type affects behavior.

Number

Numbers are simpler.

let marks = 95;
let price = 99.99;

Both are numbers.

Unlike some languages, JavaScript does not separate integers and decimals into different basic types. Everything numeric is simply treated as number.

That simplicity is one reason beginners often enjoy JavaScript early.

Boolean

Then comes boolean, one of the most useful types in programming.

A boolean stores only two possibilities:

let isLoggedIn = true;

or

let isAdmin = false;

This may look tiny, but boolean powers decision-making.

A website checks:

Is user logged in?

True → open dashboard

False → show login page

A boolean often represents choices, conditions, permissions, and yes/no logic

Undefined

Then there are special values that often confuse beginners: undefined and null.

Suppose you write:

let user;

You created a variable but gave it nothing.

If JavaScript checks it:

console.log(user);

The result becomes:

undefined

This means JavaScript knows the variable exists, but no value has arrived yet.

It is like an empty form field waiting to be filled.

NULL

But null is slightly different:

let selectedUser = null;

Here you are intentionally saying:

This variable is empty on purpose.

That difference matters in real applications because sometimes empty means "not assigned yet," and sometimes empty means "intentionally cleared."

Object

Sooner or later, a beginner meets objects.

Objects are where JavaScript starts feeling powerful.

Instead of storing one single value, an object stores related values together.

let person = {
  name: "Amit",
  age: 25
};

Now person contains multiple pieces of information inside one structure.

That means JavaScript can think in terms of real-world things: people, products, cars, accounts, orders.

A person naturally has multiple properties, so object fits perfectly.

typeof

A beautiful thing about JavaScript is that you can even ask it what type something is.

typeof "hello"

This returns:

string

And:

typeof 10

returns:

number

This becomes extremely useful when debugging because sometimes values are not what they seem

not strictly typed language

One reason JavaScript feels flexible is that variables can even change type later.

let data = 10;
data = "hello";

First it was a number.

Then it became text.

JavaScript allows this because it is dynamically typed.

Some languages are stricter, but JavaScript gives freedom.

That freedom is powerful, though it also means developers must stay careful.

JavaScript

Part 14 of 14

This JavaScript fundamentals series is designed for beginners who want to understand JavaScript in a simple and practical way. Instead of jumping directly into complex concepts, the series starts from the core building blocks of JavaScript — variables, data types, operators, conditions, loops, functions, arrays, objects, and asynchronous thinking. Each article explains one topic step by step using simple language, relatable examples, and real coding logic, so readers can build confidence gradually and understand how JavaScript actually works in real projects. Whether someone is starting coding for the first time or revising fundamentals, this series creates a strong base for modern web development

Start from the beginning

Understanding Arrays in JavaScript

A complete guide to what arrays are, how to use them, and the most important built-in methods every developer should know. What Arrays Are and Why We Need Them????? Imagine you are building a grocery