Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Updated
5 min read
A

I’m Ashwin Gudepu, a web development learner currently part of the Chai Code cohort. I’m blind, so accessibility is not an afterthought for me. I write about coding, tools, and lessons from building usable web apps.

JavaScript Variables, Data Types, Scope and Reference Types — Explained Through a Simple Story

When Rahul wrote his first line of JavaScript, he thought programming was about writing smart-looking code.

Two days later, he realized something important.

Programming is not about smart code.
It is about managing data properly.

Every application you use today — Instagram, YouTube, banking apps — is simply storing, updating, and manipulating data.

And everything starts with variables.


Chapter 1: What Is a Variable?

A variable is a named container used to store information.

Think about your phone contacts.

  • You save a name

  • You save a number

  • Later, you search using the name

JavaScript works the same way.

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

Here:

  • name stores text

  • age stores a number

Later:

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

Output:

Rahul
20

Without variables, programs cannot remember anything.


Chapter 2: Three Ways to Create Variables

Rahul discovered three keywords:

  • var

  • let

  • const

1. var — The Old Way

var city = "Chennai";

You can:

  • Change it

  • Declare it again

var city = "Chennai";
city = "Delhi";
var city = "Mumbai";

Real-life example:
It is like writing on a public notice board. Anyone can erase and rewrite.


2. let — The Safer Modern Choice

let score = 50;

You can change it:

score = 80;

But you cannot declare it again in the same block:

let score = 90; // Error

Real-life example:
Like writing in your personal notebook. You can edit it, but you cannot duplicate the same label on the same page.


3. const — The Fixed Value

const birthYear = 2005;

You cannot change it:

birthYear = 2006; // Error

Real-life example:
Your date of birth. Once recorded, it does not change.


Quick Comparison

Scope

Keyword Scope Type
var Function scoped
let Block scoped
const Block scoped

Reassignment

Keyword Can Change Value?
var Yes
let Yes
const No

Chapter 3: Understanding Scope

Scope means:

Where can I use this variable?

let age = 20;

if (age > 18) {
  let message = "You can vote";
  console.log(message);
}

console.log(message);

The last line causes an error.

Because message exists only inside the block.

Real-life example:
If a teacher writes something on a classroom board, only students inside the room can see it. Outside the room, it is not visible.


Chapter 4: Primitive Data Types

Data types tell JavaScript what kind of value is being stored.

Primitive types:

  • Store a single value

  • Are immutable

  • Are copied by value

Complete Primitive Types Table

Type Example Explanation Real-Life Example
Number let age = 20; Stores numeric values. Whole, decimal, or negative numbers. Age, price, score
String let name = "Rahul"; Stores text. Must be inside "", '' or backticks. Name, message, address
Boolean let isStudent = true; Only true or false. Used for decisions. Yes/No answers
Undefined let score; Declared but no value assigned. Empty form field
Null let user = null; Intentionally empty value. Cleared selection
Symbol let id = Symbol("id"); Creates unique value. Unique identity tag
BigInt let big = 123n; Stores very large integers. Must end with n. Scientific calculations

Copy by Value Example

let a = 10;
let b = a;

a = 20;

console.log(b);

Output:

10

Primitive values are copied independently.


Chapter 5: Reference Types

Reference types behave differently.

  • Store complex data

  • Are mutable

  • Are copied by reference

Main Reference Types

  • Object

  • Array

  • Function

Object Example

let student = {
  name: "Rahul",
  age: 20,
  isStudent: true
};

Think of an object like a student ID card containing multiple details.

Array Example

let subjects = ["Math", "Science", "English"];

Like a shopping list storing multiple items.

Function Example

function greet() {
  console.log("Hello");
}

A function is like a machine that performs a task when called.


Copy by Reference Example

let person1 = { name: "Rahul" };
let person2 = person1;

person1.name = "Amit";

console.log(person2.name);

Output:

Amit

Both variables point to the same object.

Real-life example:
Two people editing the same shared Google document. If one changes it, the other sees the change.


Primitive vs Reference

Feature Primitive Reference
Stores Single value Complex data
Mutable No Yes
Copied by Value Reference
Examples Number, String Object, Array, Function

Final Thoughts

When Rahul started learning JavaScript, everything felt confusing.

But once he understood:

  • What variables are

  • The difference between var, let, and const

  • What scope means

  • Primitive vs reference types

JavaScript stopped feeling random.

It started feeling logical.

If you truly understand these concepts, you are not just writing code.

You are controlling data.

And that is where real programming begins.

getting started with java script

Part 1 of 1

in this series of articles I will explain the esential parts of java script with clear and very beginner friendly posts