Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Updated
5 min read
Understanding Variables and Data Types in JavaScript

Hey Everyone,

In this blog, we will learn about JavaScript variables and datatypes.

When learning JavaScript, one of the first concepts you encounter is variables.

Variables help us store information so that we can use it later in our program.

What is a Variable?

Think of a variable like a box.

You can store some information inside the box, and the label helps you find it later.

Example:

Box Label → name
Value inside → "Rahul"

In JavaScript:

let name = "Rahul";

Here:

  • name → variable name (the label)

  • "Rahul" → value stored inside the variable


Why Do We Need Variables?

Variables allow us to store and reuse values.

Example:

let price = 100;
let quantity = 3;

let total = price * quantity;

console.log(total); // 300

Instead of writing numbers everywhere, variables make the code clear and reusable.


Declaring Variables in JavaScript

JavaScript provides three ways to declare variables:

  • var

  • let

  • const


1. var

var is the older way of declaring variables.

Example:

var city = "Delhi";

console.log(city);

You can change the value later.

var city = "Delhi";

city = "Mumbai";

console.log(city); // Mumbai

2. let

let is the modern way of declaring variables.

Example:

let age = 20;

console.log(age); //20

You can also change the value later.

let age = 20;

age = 21;

console.log(age); //21

3. const

const is used for values that should not change.

Example:

const country = "India";

console.log(country);

Trying to change it will cause an error.

const country = "India";

country = "USA";

This will produce an error because const values cannot be reassigned.


Primitive Data Types in JavaScript

JavaScript has several primitive data types.

Let’s look at the most common ones.


1. String

A string represents text.

Example:

let name = "Abhi";

2. Number

Numbers represent numeric values.

Example:

let age = 24;
let price = 919.99;

3. Boolean

Booleans represent true or false values.

Example:

let isStudent = true;

4. null

null means a variable intentionally has no value.

Example:

let selectedUser = null;

It means the variable exists but currently does not hold any value.


5. undefined

undefined means a variable was created, but no value has been assigned yet.

Example:

let score;

console.log(score); // undefined

Basic Difference Between var, let, and const

Feature var let const
Can change value Yes Yes No
Modern usage Rare Recommended Recommended
Can redeclare variable Yes No No

In modern JavaScript:

  • Use let for values that may change

  • Use const for values that should remain constant


What is Scope?

Scope determines where a variable can be accessed in the code.

Think of scope like rooms in a house.

If a variable is created inside a room, it can only be used inside that room.

Example:

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

Inside the block → it works.

But outside:

{
  let message = "Hello";
}

console.log(message);

This will cause an error, because the variable only exists inside the block.


Simple Scope Visualization

Global Scope
   |
   |---- variable A
   |
   |---- Block Scope
           |
           |---- variable B

variable B can only be accessed inside the block.

let and const are block scope

VAR is a global scope?

var is function-scoped, not truly global.

If you declare it inside a function, it stays inside that function.

function test() {
  var x = 10;
}

console.log(x); // Error

x cannot be accessed outside the function.

However, if var is declared outside any function, then it becomes global.

var x = 10;

console.log(x); // works

So var is often seen as global because many beginners declare it outside functions.


Practice Assignment

Try this in your browser console.


Step 1: Declare Variables

let name = "Rahul";
let age = 20;
let isStudent = true;

Step 2: Print the Values

console.log(name);
console.log(age);
console.log(isStudent);

Step 3: Change a let Value

age = 21;
console.log(age);

Observe how the value changes.


Step 4: Try Changing const

const country = "India";

country = "USA";

Observe the error JavaScript gives.


And now, you know what variables and data types in JS are.

If you have any doubt or want to connect, feel free to drop a comment — I’d be happy to help.

Thanks for reading, and see you in the next blog!

Peace ✌️ and Happy Learning!