Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Updated
•4 min read•View as Markdown

JavaScript Variables and Data Types (Beginner Friendly Guide)

Every program needs a way to store information.
In JavaScript, this is done using variables.

In this guide we will learn:

  • What variables are

  • How to declare variables using var, let, and const

  • Primitive data types (string, number, boolean, null, undefined)

  • The difference between var, let, and const

  • What scope means (simple explanation)


1. What Are Variables?

Think of a variable like a box that stores information.

Example:

Box label → Name
Inside box → "Tushar"

In JavaScript:

let name = "Tushar";

Here:

  • name → the box

  • "Tushar" → the value stored in the box

Variables store data so it can be used or changed later in the program. (GeeksforGeeks)


2. Declaring Variables in JavaScript

JavaScript allows three ways to create variables:

  • var

  • let

  • const

Example:

var city = "Delhi";
let age = 22;
const country = "India";

These keywords tell JavaScript to reserve memory to store values. (CoreLangs.org)


3. Primitive Data Types

The type of data stored inside a variable is called a data type.

Here are the most common primitive types.


1. String (text)

Used to store text.

let name = "Tushar";

Examples:

"Hello"
"JavaScript"
"My name is Tushar"

Strings represent text data. (Cratecode)


2. Number

Used to store numbers.

let age = 21;
let price = 99.99;

Numbers can be:

  • integers → 10

  • decimals → 3.14


3. Boolean

Boolean means true or false.

let isStudent = true;

Example uses:

isLoggedIn = true
isAdmin = false

4. Undefined

A variable declared but not assigned a value becomes undefined.

let score;
console.log(score);

Output:

undefined

5. Null

null means intentional empty value.

let selectedUser = null;

This means:

The variable exists
But currently has no value

4. Difference Between var, let, and const

These keywords behave differently.

Example

var x = 10;
let y = 20;
const z = 30;

Comparison Table

Keyword

Can change value?

Scope

Notes

var

Yes

Function scope

Old way

let

Yes

Block scope

Modern JS

const

No

Block scope

Value cannot be reassigned

var variables are function-scoped, while let and const are block-scoped and preferred in modern JavaScript. (GeeksforGeeks)


5. Changing Variable Values

Using let

Values can change.

let age = 20;
age = 21;
console.log(age);

Output

21

Using const

Values cannot be reassigned.

const pi = 3.14;
pi = 3.1415; // Error

const variables must be assigned when created and cannot be reassigned later. (techtestlog.com)


6. What Is Scope? (Beginner Explanation)

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

Example:

{
let message = "Hello";

  console.log(message);

}

console.log(message); // Error

Why?

Because message only exists inside the block { }.

Simple idea:

Scope = area where a variable is accessible

Types (simple view):

Global scope → accessible everywhere
Local scope → accessible only inside a block or function

Scope Visualization

Program
│

├── Global variables

│

└── Code block

     └── Local variables

Quick Summary

Concept

Meaning

Variable

A container for storing data

var

Old way of declaring variables

let

Variable that can change

const

Variable that cannot be reassigned

string

Text data

number

Numeric values

boolean

true or false

null

intentionally empty value

undefined

declared but not assigned

scope

where a variable can be used


💡 Best practice in modern JavaScript

Use const by default
Use let when value needs to change
Avoid var