Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

What Are Variables and Why We Need Them:-

Updated
4 min read
Understanding Variables and Data Types in JavaScript

Introduction

Think of a variable as a box where you can store information. For example, you might have a box labeled “Name” where you keep your name. In programming, variables let us store information like names, numbers, or true/false values so we can use and change them later in our code.

What Are Variables and Why We Need Them

Variables are containers for storing data values. Instead of hardcoding values everywhere, we can store them in variables and use them multiple times.

Example:

let name = "Pratham";
let age = 21;
let isStudent = true;

console.log(name); // Pratham
console.log(age); // 21
console.log(isStudent); // true

Declaring Variables: var, let, and const

In JavaScript, there are three ways to declare variables:

Keyword Can Reassign? Scope Example
var Yes Function-scoped var name = "Pratham";
let Yes Block-scoped let age = 21;
const No Block-scoped const isStudent = true;

Key Notes:

  • var is old and function-scoped. Avoid using it in modern code unless necessary.

  • let is modern and block-scoped. You can change its value.

  • const is also block-scoped, but its value cannot change once assigned.

Example of Changing Values

let age = 21;
age = 22; // ✅ Works

const isStudent = true;
isStudent = false; // ❌ Error: Assignment to constant variable

Primitive Data Types

JavaScript has simple or “primitive” data types:

Data Type Example Description
String "Pratham" Text
Number 21 Numeric value
Boolean true True or false
Null null Empty or unknown value
Undefined undefined Variable declared but not assigned

Example:

let name = "Pratham";   // string
let age = 21;           // number
let isStudent = true;   // boolean
let city = null;        // null
let school;             // undefined

Understanding Scope (Beginner-Friendly)

Scope is like the accessibility of a variable. Think of it as rooms in a house:

  • Global Scope: A variable declared outside any function or block can be accessed anywhere (like the living room).

  • Function Scope: A variable declared inside a function is only accessible inside that function (like your bedroom).

  • Block Scope: A variable declared inside a block { ... } using let or const can only be accessed inside that block (like a closet in the bedroom).

Scope Visualization Diagram:

Global Scope
┌───────────────────────────┐
│ let globalVar = "I am global" │
│ function myFunc() {           │
│     var funcVar = "I am inside function" │
│     if(true) {                │
│         let blockVar = "I am inside block" │
│     }                         │
│ }                             │
└───────────────────────────┘

Example:

let globalVar = "I am global";

function myFunc() {
    var funcVar = "I am inside function";
    if(true) {
        let blockVar = "I am inside block";
        console.log(blockVar); // ✅ Works
    }
    // console.log(blockVar); // ❌ Error: blockVar not accessible here
}

console.log(globalVar); // ✅ Works
// console.log(funcVar); // ❌ Error: funcVar not accessible here

Practice Assignment: Play with Variables and Data Types

Task Instructions Try It
1 Declare Variables: Create variables for name, age, isStudent let name = "Alice"; let age = 20; const isStudent = true;
2 Print Values: Display them in the console console.log(name, age, isStudent);
3 Change Values: Reassign let variables and try changing const name = "Bob"; age = 21; isStudent = false; // ❌ Error
4 Experiment with Scope: Declare a variable inside a block and try accessing it outside if(true){ let blockVar="Hi"; console.log(blockVar); } console.log(blockVar); // ❌ Error

This assignment reinforces:

  • Variable declaration (var, let, const)

  • Primitive data types

  • Reassigning values

  • Basic scope