Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Updated
3 min read

What Are Variables?

Imagine a box where you store things.

  • One box stores your name

  • One box stores your age

  • One box stores whether you are a student

In programming, a variable is just like that box.

It stores information so we can use it later.

Real-Life Analogy

Think of it like this:

Box Label: Name
Inside the Box: "Rahul"

In JavaScript:

let name = "Rahul";
  • name → box label

  • "Rahul" → value inside the box

How to Declare Variables

In JavaScript, we use:

  • var

  • let

  • const

Using let

Used when the value can change later.

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

age = 22;    // value changed
console.log(age);

Output:

21
22

We can update let variables.

Using const

Used when the value should not change.

const country = "India";
console.log(country);

country = "USA";    // Error

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

✔ Use const when value should remain fixed.

Using var

Older way of declaring variables.

var city = "Mumbai";
console.log(city);

It works, but modern JavaScript prefers let and const.

Primitive Data Types

Data types describe what kind of data we are storing.

String (Text)

Used for names, messages, words.

let name = "Vishnu";

Anything inside quotes " " or ' ' is a string.

Number

Used for numeric values.

let age = 25;
let price = 199.99;

Boolean (True or False)

Used for yes/no type values.

let isStudent = true;
let isLoggedIn = false;

Null

Means intentionally empty.

let result = null;

It means: “There is no value right now.”

Undefined

When a variable is declared but not given a value.

let score;
console.log(score);

Output:

undefined

Basic Difference Between var, let, and const

Feature var let const
Can change value? Yes Yes No
Modern usage? Rare Yes Yes
Preferred today? No Yes Yes

Simple Rule for Beginners:

  • Use const by default

  • Use let if value needs to change

  • Avoid var for now

What is Scope?

Scope means: Where can we use the variable?

Example

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

console.log(message);        // Error

Why?

Because let works only inside the block { }.

That area is called block scope.

var behaves differently

{
    var greeting = "Hi";
}

console.log(greeting);    // Works

This is one reason var can cause confusion.

Showcasing Value Change

Using let

let marks = 50;
marks = 75;
console.log(marks);    // 75 - Allowed

Using const

const pi = 3.14;
pi = 3.14159;    // Error - Not Allowed

Assignment

  1. Declare variables for:

    • Name

    • Age

    • IsStudent

  2. Print them in the console

  3. Try changing values for let and const and observe behavior

Solutions

Performing using let

let name = "Visshnnu Tejaa"
let age = 27
let isStudent = true

console.log(name)    // Visshnnu Tejaa
console.log(age)     // 27
console.log(isStudent) // true

Performing using const

const name = "Visshnnu Tejaa"
const age = 27
const isStudent = true

console.log(name)    // Error
console.log(age)     // Error
console.log(isStudent) // Error