JavaScript Variables and Data Types (Beginner Guide)

Published: (March 4, 2026 at 07:11 AM EST)
4 min read
Source: Dev.to

Source: Dev.to

What Are Variables?

Think of a variable like a box that stores information.

  • One box stores your name
  • Another stores your age
  • Another stores whether you are a student

In programming, we store information inside variables so the computer can use it later.

let name = "Rahul";
let age = 18;
  • name stores "Rahul"
  • age stores 18

So a variable is simply a container for storing data.

How to Declare Variables in JavaScript

In JavaScript, we can create variables using three keywords:

  • var
  • let
  • const

Using var

var city = "Delhi";

var was used in older JavaScript versions. It still works but is less recommended today.

Using let

let age = 20;

let is commonly used when the value may change later.

let score = 10;
score = 15; // now score becomes 15

Using const

const country = "India";

const means constant, which means the value cannot be changed later.

const pi = 3.14;
// Trying to change it will cause an error

Primitive Data Types in JavaScript

A data type describes what kind of value a variable stores. JavaScript has several primitive data types.

String

A string stores text.

let name = "Aman";
let city = "Mumbai";
let message = "Hello World";

Strings are written inside quotes ("" or '').

Number

A number stores numeric values.

let age = 18;
let price = 99.99;

Numbers can be:

  • Whole numbers → 20
  • Decimal numbers → 10.5

Boolean

A boolean stores only two values:

  • true
  • false
let isStudent = true;
let isLoggedIn = false;

This type is commonly used for conditions and decisions.

Null

null represents an empty value.

let result = null;

It means the variable exists but currently has no value.

Undefined

A variable is undefined when it is declared but not given a value.

let score;

Since we didn’t assign anything, the value is undefined.

Difference Between var, let, and const

KeywordCan Change Value?Modern Usage
varYesMostly avoided
letYesRecommended
constNoRecommended for fixed values
let age = 18;
age = 19; // allowed

const country = "India";
country = "USA"; // error

What is Scope? (Beginner Explanation)

Scope means where a variable can be used in your code.

Think of scope like rooms in a house. If you keep a box in your bedroom, it can only be accessed in that room. Similarly, some variables can only be used inside certain parts of the code.

{
  let message = "Hello";
  console.log(message); // works inside the block
}

Here message works inside the block, but outside it cannot be used. This helps keep code organized and safe.

Practical Example

let name = "Rohan";
let age = 19;
let isStudent = true;

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

Output in console

Rohan
19
true

Assignment Practice

Write the following code and experiment with it:

let name = "Rahul";
let age = 18;
let isStudent = true;

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

Now try:

  • Changing the value of name
  • Changing the value of age
  • Creating a const variable and attempting to change it
const country = "India";
country = "USA"; // observe the error

Observe how JavaScript behaves.

0 views
Back to Blog

Related posts

Read more »

The 'skill-check' JS quiz

Question 1: Type coercion What does the following code output to the console? javascript console.log0 == '0'; console.log0 === '0'; Answer: true, then false Ex...