Skip to main content

Command Palette

Search for a command to run...

Understanding Variables and Data Types in JavaScript

Updated
•4 min read•View as Markdown

When I started learning JavaScript, one of the first things I had to understand was variables. At first it felt confusing, but after writing a few examples it became much clearer.

A simple way to think about a variable is like a box that stores information. Instead of remembering values directly, we give them a name and store them in a variable.

For example, instead of remembering a number like 20, we can store it inside a variable called age.

let age = 20;

Here:

  • age is the variable name

  • 20 is the value stored inside the variable

Later in the program we can use age whenever we need that value.

Why Variables Are Needed

Variables are useful because programs usually need to store and reuse data.

For example, a program may need to store:

  • a person's name

  • their age

  • whether they are a student

  • some calculated result

Without variables, we would have to keep writing the same values repeatedly.

Example:

let name = "john";
let age = 19;

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

Output:

john
19

So variables help us store and reuse information easily.

Declaring Variables in JavaScript

In JavaScript we can declare variables using three keywords:

  • var

  • let

  • const

They all create variables, but they behave slightly differently.

Using var

var was used in older JavaScript.

Example:

var city = "Delhi";

console.log(city);

Output:

Delhi

The value of a var variable can be changed later.

var city = "Delhi";

city = "Mumbai";

console.log(city);

Output:

Mumbai

Nowadays most developers prefer let and const instead of var.

Using let

let is used when the value may change later.

Example:

let score = 10;

console.log(score);

Now we change it:

score = 20;

console.log(score);

Output:

20

So let allows us to update the value.

Using const

const is used when the value should not change.

Example:

const country = "India";

console.log(country);

Now if we try to change it:

country = "USA";

JavaScript will give an error, because const variables cannot be reassigned.

So remember:

  • let → value can change

  • const → value should stay the same

Primitive Data Types in JavaScript

Variables store values, and each value has a data type. Data types simply describe what kind of data is stored.

JavaScript has several primitive data types, but beginners usually start with these five.

1. String

A string represents text.

Example:

let name = "Abdulla";

Here "Abdulla" is a string.

Other examples:

let city = "Mangalore";
let course = "JavaScript";

Strings are usually written inside quotes.

2. Number

Numbers represent numeric values.

Example:

let age = 19;
let marks = 85;

JavaScript does not separate integers and decimals like some languages.

let price = 99.99;

Both are still considered number type.

3. Boolean

A boolean value can only be true or false.

Example:

let isStudent = true;
let isLoggedIn = false;

These are often used in conditions and logic.

4. Null

null means the variable intentionally has no value.

Example:

let result = null;

This usually means the value will be assigned later.

5. Undefined

undefined means a variable has been declared but no value has been assigned yet.

Example:

let data;

console.log(data);

Output:

undefined

This happens because the variable exists, but nothing has been stored in it.

Basic Difference Between var, let, and const

Here is a simple comparison:

Keyword Can change value? Used today?
var Yes Rarely used
let Yes Very common
const No Very common

Most modern JavaScript code mainly uses let and const.

What is Scope?

Scope simply means where a variable can be accessed in the code.

For beginners, the easiest way to understand scope is to think of it like a variable being available only inside a certain block of code.

Example:

let number = 10;

if (number > 5) {
  let message = "Number is greater than 5";
  console.log(message);
}

Inside the block, message works fine.

But if we try to use it outside:

console.log(message);

It will give an error.

This happens because message was created inside that block only.

So in simple terms:

Scope means where a variable can be used in the program

Final Thoughts

When starting with JavaScript, understanding variables and data types is very important. Almost every program you write will involve storing and using data..