Back to blog
JavaScript

Understanding Variables and Data Types in JavaScript

May 10, 2026 2 min read

What Are Variables?

Variables store information in memory.

Real-life analogy:

Variable = labeled box storing data

Example:

  • Name box → "Ali"

  • Age box → 22


Why Variables Are Needed

Variables help store and reuse data.

Without variables:

console.log("Ali");
console.log("Ali");
console.log("Ali");

With variables:

const name = "Ali";

console.log(name);
console.log(name);


Declaring Variables

JavaScript provides:

  • var

  • let

  • const


Using let

let allows changing values later.


Example

let age = 20;

console.log(age);

age = 21;

console.log(age);


Output

20
21


Using const

const creates variables whose value cannot be reassigned.


Example

const country = "India";

console.log(country);


Trying to Change const

const country = "India";

country = "USA";


Result

Error


Using var

var is the older way of declaring variables.


Example

var city = "Lucknow";

console.log(city);

Modern JavaScript usually prefers:

  • let

  • const


Difference Between var, let, and const

KeywordCan Reassign?Modern Usage
varYesRare
letYesCommon
constNoVery Common

Primitive Data Types

Primitive data types are basic value types in JavaScript.


1. String

Used for text.


Example

const name = "Ali";


2. Number

Used for numeric values.


Example

const age = 22;


3. Boolean

Represents true or false.


Example

const isStudent = true;


4. Undefined

Variable declared but no value assigned.


Example

let phoneNumber;

console.log(phoneNumber);


Output

undefined


5. Null

Represents intentional empty value.


Example

const selectedUser = null;


Data Type Examples

DataType
"Ali"String
25Number
trueBoolean
undefinedUndefined
nullNull

Checking Data Types

Use:

typeof


Example

console.log(typeof "Ali");
console.log(typeof 25);
console.log(typeof true);


Output

string
number
boolean


What Is Scope?

Scope means:

Where a variable can be accessed


Simple Scope Example

let message = "Hello";

function test() {
  console.log(message);
}

test();

message is accessible inside the function.


Block Scope Example

{
  let age = 20;

  console.log(age);
}

Works inside the block.


Outside Block

{
  let age = 20;
}

console.log(age);


Result

Error


Scope Visualization

Diagram: flowchart TD

Practical Example

const name = "Sara";
let age = 19;
let isStudent = true;

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


Assignment

Step 1: Declare Variables

const name = "Ali";
let age = 22;
let isStudent = true;


Step 2: Print Values

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


Step 3: Change let

age = 23;

console.log(age);


Step 4: Try Changing const

name = "Ahmed";

Observe the error.


Key Takeaways

ConceptSummary
VariableStores data
letReassignable variable
constCannot reassign
varOlder variable keyword
StringText data
NumberNumeric data
Booleantrue/false
ScopeWhere variable is accessible

Final Notes

Variables and data types are the foundation of JavaScript.

Almost everything in programming depends on:

  • Storing data

  • Modifying data

  • Accessing data correctly

Mastering these basics makes learning functions, arrays, objects, and APIs much easier.


0 Comments

Sign in to join the conversation

No comments yet. Be the first to comment!