Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Published
4 min read
Understanding Variables and Data Types in JavaScript

When starting with JavaScript, one of the first things you learn is variables and data types. These are fundamental concepts that help you store and manage information in your programs.

Think of variables as containers that hold information, and data types as the kind of information inside those containers.

1. What Variables Are and Why They Are Needed

Imagine you have a box where you store something important — maybe your phone, keys, or wallet. Instead of holding them in your hand all the time, you put them in a box so you can use them later.

In programming, variables work like that box.

They store information so your program can use it later, update it, or display it.

For example, a website might need to store:

  • A user's name

  • Their age

  • Whether they are logged in

In JavaScript, we store those values using variables.

let name = "Rahul";
let age = 25;
let isLoggedIn = true;

Your program can now reuse these values anywhere they are needed.

2. How to Declare Variables Using var, let, and const

In JavaScript, there are three ways to create variables:

  • var

  • let

  • const

Each one declares a variable, but they behave slightly differently.

Using var

var city = "delhi";

var was the original way to create variables in JavaScript.

However, modern JavaScript usually prefers let and const because they behave more predictably.

Using let

let age = 21;

let creates a variable whose value can change later.

Example:

let score = 10;

score = 20;

console.log(score); // 20

Here we changed the value from 10 to 20.

Using const

const country = "India";

const means the value cannot be reassigned later.

Example:

const pi = 3.14;

pi = 3.14159; // This will cause an error

Because const variables are meant to stay constant.

3. Primitive Data Types in JavaScript

Every variable stores some kind of data. The type of data stored is called a data type.

JavaScript has several primitive data types, but beginners usually start with these five:

  • String

  • Number

  • Boolean

  • Null

  • Undefined

String

A string represents text.

Examples include:

  • names

  • messages

  • addresses

Example:

let name = "Aman";
let message = "Welcome to my website";

Strings are written inside quotes.

Number

A number represents numeric values.

Example:

let age = 25;
let price = 199.99;

Numbers can be:

  • whole numbers (10)

  • decimal numbers (3.14)

Boolean

A boolean represents only two possible values:

  • true

  • false

Example:

let isLoggedIn = true;
let hasPermission = false;

Booleans are often used for conditions and decisions.

Null

null represents an intentional empty value.

It means the variable exists but currently has no value assigned.

Example:

let selectedUser = null;

You might assign a value later.

Undefined

undefined means a variable has been declared but not given a value yet.

Example:

let username;

console.log(username); // undefined

The variable exists, but nothing has been stored inside it yet.

4. Basic Difference Between var, let, and const

Keyword Can Change Value Modern Usage
var Yes mostly avoided
let yes commonly used
const No (cannot reassign) Very common

example:

var a = 10;
let b = 20;
const c = 30;

a = 15; // allowed
b = 25; // allowed
c = 35; // error

5. What Is Scope (Beginner-Friendly Explanation)

Scope determines where a variable can be used in your code.

Think of scope like rooms in a house.

If you keep something in your bedroom, people in the living room cannot access it.

Similarly, variables defined in one place may not be accessible everywhere.

Example

function greet() {
  let message = "Hello!";
  console.log(message);
}

greet();

Here:

  • message exists inside the function

  • It cannot be used outside that function

Scope helps keep code organized and prevents conflicts between variables.