Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Updated
2 min read

What are Variables?

Think of a variable like a box that stores information.

The box name = variable name
The thing inside the box = value

For example:

A box labeled "name" can store "Arnab"
A box labeled "age" can store 20

Declaring Variables in JavaScript :

var
let
const

1. Using var

var name = "Arnab";
console.log(name);

2. Using let

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

The value of a let variable can be changed later:

age = 21;
console.log(age);

3. Using const

1. Using const

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

The value of a const variable cannot be changed:

country = "USA" ; // ❌ Error

Primitive Data Types

JavaScript has some basic data types you’ll use daily:

1. String (Text)

Used for names, messages, etc.

let name = "Arnab" ;

2. Number

Used for age, marks, money, etc.
let age = 20 ;

3. Boolean (True/False)

Used for yes/no type values.

let isStudent = true;

4. Null (Empty Value on Purpose)

let result = null;

5. Undefined (No Value Assigned)

let marks ;
console.log(marks) ;

Basic Difference Between var, let, and const

What Is Scope?

Scope means where you can use a variable.

Think of it like a classroom :

  • If you speak loudly → everyone hears (global scope)

  • If you whisper to a friend → only they hear (local scope)

Example:

let name = "Arnab" ; // global
function greet( ) {
let message = "Hello" ; // local
console.log(message);
}
console.log(name) ;

message works only inside the function.
name works everywhere.

How Values Change (Except const)

let score = 50;
console.log(score) ;

score = 80;
console.log(score) ;

Works because it's let.

But:

const score = 50;
score = 80; // ❌ Error

Diagram Ideas

  1. Comparison table of var, let, and const

  2. Simple scope visualization diagram