Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Learn how JavaScript stores information using variables and simple data types

Published
6 min read
Understanding Variables and Data Types in JavaScript
G
I enjoy blending technology with business to build solutions that create real impact. I’m fascinated by how technology empowers people, businesses, and startups. I believe thoughtful use of technology quietly improves lives, and that belief fuels everything I build and explore 💻.

When someone starts learning programming, one of the first concepts they encounter is variables. At first the word might sound technical, but the idea behind it is actually very simple. Variables help us store information so that our program can use it later.

Think of a variable as a box that stores some data. The box has a label on it, and inside the box we place a value. Whenever the program needs that value, it simply looks at the box using its label.

For example, imagine writing down someone's name, age, and whether they are a student. Instead of repeatedly typing the values everywhere in the program, we store them inside variables and reuse them whenever needed.

This makes programs easier to manage and understand.

Why Variables Are Needed

Programs constantly work with information. A website might store a user's name, a game might track the player's score, and a shopping application might calculate the total price of items.

Without variables, we would have to repeat the same values again and again throughout the program. That would quickly make the code messy and difficult to maintain.

Variables solve this problem by allowing us to store information once and reuse it multiple times.

For example, if we store a person's name in a variable, we can use that name anywhere in our program. If the value changes, we only need to update it in one place.

Declaring Variables in JavaScript

In JavaScript there are three common ways to declare variables. These are var, let, and const.

Let us look at a simple example of declaring variables.

var city = "Indore"
let age = 21
const country = "India"

console.log(city)
console.log(age)
console.log(country)

Output

Indore

21

India

In this example we created three variables and stored values in them. Each variable has a name and a value.

The console.log statements print the stored values to the console.

Although all three keywords can declare variables, they behave slightly differently. We will understand those differences shortly.

Understanding Primitive Data Types

Whenever we store information in a variable, that value belongs to a certain type. These are called data types.

JavaScript has several data types, but beginners usually start with a few basic ones.

One common type is a string. Strings represent text values.

Example

let name = "Rohit"

console.log(name)

Output

Rohit

Here the variable name stores a piece of text, so its type is string.

Another common type is number. Numbers represent numeric values.

let age = 20

console.log(age)

Output

20

In this example the variable age stores a numeric value.

Another simple type is boolean. A boolean represents either true or false.

let isStudent = true

console.log(isStudent)

Output

true

Boolean values are often used when checking conditions such as whether a user is logged in or whether a task is completed.

JavaScript also has two special data types called null and undefined.

Null represents an intentional empty value.

let middleName = null

console.log(middleName)

Output

null

Undefined represents a variable that has been declared but not assigned any value.

let score

console.log(score)

Output

undefined

Understanding these basic data types helps in choosing the right type of value for each variable.

Basic Difference Between var, let, and const

All three keywords var, let, and const are used to create variables, but there are a few important differences between them.

The var keyword is the older way of declaring variables in JavaScript. It is still supported but modern JavaScript usually prefers let and const.

The let keyword allows us to declare a variable whose value can change later.

Example

let score = 10
console.log(score)

score = 15
console.log(score)

Output

10

15

Here the value of score was updated successfully.

The const keyword is used when the value should not change after being assigned.

Example

const birthYear = 2004

console.log(birthYear)

Output

2004

If we try to change the value of a const variable, JavaScript throws an error.

const birthYear = 2004

birthYear = 2005

Output

Error because a const value cannot be reassigned

Because of this behavior, const is often used when we want to ensure that a value remains constant throughout the program.

Understanding Scope in a Simple Way

Scope refers to where a variable can be accessed in a program.

You can think of scope as the area where a variable is visible and usable.

Consider the following example.

let message = "Hello JavaScript"

console.log(message)

Output

Hello JavaScript

Here the variable message is available in the entire script.

Now look at this example using a block of code.

if (true) {
    let greeting = "Welcome"
    console.log(greeting)
}

console.log(greeting)

Output

Welcome

Error because greeting cannot be accessed outside the block

The variable greeting only exists inside the block where it was created. Outside that block it is not available.

This idea helps prevent variables from interfering with each other in larger programs.

A Small Practice Exercise

The best way to understand variables is to try creating them yourself.

Let us create three variables that represent a person's details.

let name = "Aarav"
let age = 19
let isStudent = true

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

Output

Name: Aarav

Age: 19

Is Student: true

Now try changing the value of the age variable.

age = 20

console.log("Updated Age:", age)

Output

Updated Age: 20

The value changes successfully because the variable was declared using let.

Now try the same with const.

const country = "India"

country = "USA"

Output

Error because const values cannot be reassigned

This small experiment helps demonstrate how variables behave differently depending on how they are declared.

Variables are one of the most fundamental parts of programming. They allow us to store, update, and reuse information throughout a program. Once you become comfortable using variables and understanding basic data types, many other JavaScript concepts start becoming easier to learn.

Understanding how variables work is an important step toward writing organized and readable programs.