DEV Community

Cover image for Understanding Variables and Data Types in JavaScript
Subhrangsu Bera
Subhrangsu Bera

Posted on

Understanding Variables and Data Types in JavaScript

If you're starting your JavaScript journey, one of the first concepts you'll encounter is Variables and Data Types.

They are the foundation of everything in programming — from storing a user's name to handling complex application state.

In this guide, we'll break down:

  • What variables are
  • How var, let, and const work
  • JavaScript primitive data types
  • How JavaScript stores and copies values

All explained with simple examples and real code outputs.

So, let's start...

Imagine you are moving into a brand-new house. You have a truckload of items: furniture, clothes, kitchen gadgets, and some very fragile family heirlooms. To stay sane, you don’t just throw everything into the middle of the living room. You use boxes, and more importantly, you label them.

In the world of JavaScript, Variables are those labeled boxes, and Data Types are the specific items you put inside them. Understanding how to use these boxes correctly is the first step to becoming a master architect of the web.

1. The Labeled Boxes: What are Variables?

In programming, variables are containers used for storing data values. They allow us to give a name to a piece of information so we can refer to it, move it around, or change it later in our code.

Example

let userName = "Subhrangsu";
let age = 25;
let isDeveloper = true;

console.log(userName);
console.log(age);
console.log(isDeveloper);
Enter fullscreen mode Exit fullscreen mode

Output

Subhrangsu
25
true
Enter fullscreen mode Exit fullscreen mode

Here:

  • userName → stores a string
  • age → stores a number
  • isDeveloper → stores a boolean

The Rules of the Label (Naming Conventions)

Before you start labeling your boxes, JavaScript has a few rules you must follow.

Case Sensitivity

let myVariable = "Hello";
let myvariable = "World";

console.log(myVariable);
console.log(myvariable);
Enter fullscreen mode Exit fullscreen mode

Output

Hello
World
Enter fullscreen mode Exit fullscreen mode

These are two different variables.

Characters Allowed

let user_name = "Subhrangsu";
let $price = 100;
let _count = 10;

console.log(user_name, $price, _count);
Enter fullscreen mode Exit fullscreen mode

Output

Subhrangsu 100 10
Enter fullscreen mode Exit fullscreen mode

Note: Reserved keywords cannot be used as variable names.

The Pro Tip: camelCase

let isUserLoggedIn = true;
let totalPriceOfItems = 250;

console.log(isUserLoggedIn);
console.log(totalPriceOfItems);
Enter fullscreen mode Exit fullscreen mode

Output

true
250
Enter fullscreen mode Exit fullscreen mode

This naming style improves readability.

2. Meet the Three Label Makers: var, let, and const

Not all labels are created equal. Depending on which label you use, your variable behaves differently.

The Old Reliable: var

The traditional method, var, is function-scoped.
If declared outside a function, it becomes global.

var city = "Kolkata";

console.log(city);
Enter fullscreen mode Exit fullscreen mode

Output

Kolkata
Enter fullscreen mode Exit fullscreen mode

But var has a problem — it ignores block scope.

Example:

if (true) {
    var hobby = "Coding";
}

console.log(hobby);
Enter fullscreen mode Exit fullscreen mode

Output

Coding
Enter fullscreen mode Exit fullscreen mode

Even though it was declared inside the block, it is still accessible outside.

The Modern Standard: let

Introduced in 2015 (ES6), Introduced in ES6 (2015), let is used for variables whose values may change. It is block-scoped, meaning the variable only exists within the specific set of curly braces {} where you created it.

let age = 30;

console.log(age);

age = 31;

console.log(age);
Enter fullscreen mode Exit fullscreen mode

Output

30
31
Enter fullscreen mode Exit fullscreen mode

Example with block scope:

if (true) {
    let message = "Hello";
    console.log(message);
}

console.log(message);
Enter fullscreen mode Exit fullscreen mode

Output

Hello
ReferenceError: message is not defined
Enter fullscreen mode Exit fullscreen mode

If a variable is declared but not assigned a value, JavaScript automatically assigns undefined.

let a;
console.log(a); // undefined

a = 45;
console.log(a); // 45
Enter fullscreen mode Exit fullscreen mode

The Permanent Vault: const

const is for constants. Once you assign a value to a const box, you cannot reassign it to something else. It is also block-scoped.

Important nuance: While you cannot reassign the entire object or array stored in a const variable, you can still modify the values inside it.

const myCity = "New York";

console.log(myCity);
Enter fullscreen mode Exit fullscreen mode

Output

New York
Enter fullscreen mode Exit fullscreen mode

Trying to change it:

const myCity = "New York";

myCity = "London";
Enter fullscreen mode Exit fullscreen mode

Output

TypeError: Assignment to constant variable
Enter fullscreen mode Exit fullscreen mode

Important Nuance with Objects

const hero = {
    name: "Spider-Man",
    city: "New York",
};

hero.name = "Iron Man";

console.log(hero);
Enter fullscreen mode Exit fullscreen mode

Output

{ name: 'Iron Man', city: 'New York' }
Enter fullscreen mode Exit fullscreen mode

You cannot replace the object, but you can modify its properties.

3. What is Scope? (Your Variable's Neighborhood)

Scope is the context in which a variable is visible.

Global Scope

let greeting = "Hello World";

function showMessage() {
    console.log(greeting);
}

showMessage();
Enter fullscreen mode Exit fullscreen mode

Output

Hello World
Enter fullscreen mode Exit fullscreen mode

The variable is accessible everywhere.

Local / Block Scope

function testScope() {
    let secret = "Hidden Message";
    console.log(secret);
}

testScope();

console.log(secret);
Enter fullscreen mode Exit fullscreen mode

Output

Hidden Message
ReferenceError: secret is not defined
Enter fullscreen mode Exit fullscreen mode

The variable only exists inside the function.

4. Understanding Data Types: What’s in the Box?

JavaScript data types are generally divided into two categories:

  1. Primitive
  2. Non-Primitive

Primitive Data Types

Primitive values are immutable, meaning their values cannot be changed once created.
This means operations on primitive values always produce a new value rather than modifying the original one.

String

let name = "Subhrangsu";

console.log(name);
console.log(typeof name);
Enter fullscreen mode Exit fullscreen mode

Output

Subhrangsu
string
Enter fullscreen mode Exit fullscreen mode

Number

let age = 25;
let price = 199.99;

console.log(age);
console.log(typeof age);
Enter fullscreen mode Exit fullscreen mode

Output

25
number
Enter fullscreen mode Exit fullscreen mode

Boolean

let isStudent = true;

console.log(isStudent);
console.log(typeof isStudent);
Enter fullscreen mode Exit fullscreen mode

Output

true
boolean
Enter fullscreen mode Exit fullscreen mode

Null

let weatherCondition = null;

console.log(weatherCondition);
console.log(typeof weatherCondition);
Enter fullscreen mode Exit fullscreen mode

Output

null
object
Enter fullscreen mode Exit fullscreen mode

Note: typeof null returns "object" due to a historical bug in JavaScript that has been kept for backward compatibility.

Undefined

let userInput;

console.log(userInput);
console.log(typeof userInput);
Enter fullscreen mode Exit fullscreen mode

Output

undefined
undefined
Enter fullscreen mode Exit fullscreen mode

Symbol

const uniqueId = Symbol("user");

console.log(uniqueId);
console.log(typeof uniqueId);
Enter fullscreen mode Exit fullscreen mode

Output

Symbol(user)
symbol
Enter fullscreen mode Exit fullscreen mode

BigInt

const bigNumber = 9007199254740991n;

console.log(bigNumber);
console.log(typeof bigNumber);
Enter fullscreen mode Exit fullscreen mode

Output

9007199254740991n
bigint
Enter fullscreen mode Exit fullscreen mode

Checking the Type with typeof

console.log(typeof "Hello");
console.log(typeof 42);
console.log(typeof true);
console.log(typeof null);
console.log(typeof undefined);
Enter fullscreen mode Exit fullscreen mode

Output

string
number
boolean
object
undefined
Enter fullscreen mode Exit fullscreen mode

5. Copying Values: The Beginner's Trap

Understanding how JavaScript copies values is very important because it affects how data behaves when assigned to another variable.

Primitive → Copy by Value

let hero = "Batman";
let copiedHero = hero;

copiedHero = "Superman";

console.log(hero);
console.log(copiedHero);
Enter fullscreen mode Exit fullscreen mode

Output

Batman
Superman
Enter fullscreen mode Exit fullscreen mode

They are independent copies.

Objects → Copy by Reference

let heroStats = { strength: 80 };

let copiedStats = heroStats;

copiedStats.strength = 100;

console.log(heroStats);
console.log(copiedStats);
Enter fullscreen mode Exit fullscreen mode

Output

{ strength: 100 }
{ strength: 100 }
Enter fullscreen mode Exit fullscreen mode

Both variables point to the same object in memory.

Deep Copy Example

let original = { power: 50 };

let clone = { ...original };
clone.power = 90;

console.log(original);
console.log(clone);
Enter fullscreen mode Exit fullscreen mode

Output

{ power: 50 }
{ power: 90 }
Enter fullscreen mode Exit fullscreen mode

Now they are independent objects.
For simple objects, the spread operator can be used. For deep nested objects, structuredClone() is safer.

6. Practical Lab: Observe the Behavior

const studentName = "Subhrangsu";
let studentAge = 25;
let isGraduated = false;

console.log("Name:", studentName, "| Type:", typeof studentName);
console.log("Age:", studentAge, "| Type:", typeof studentAge);

// studentName = "Subha"
// TypeError: Assignment to constant variable.

studentAge = 26;

console.log("New Age:", studentAge);

let graduationYear = null;

console.log("Graduation Year:", graduationYear);
Enter fullscreen mode Exit fullscreen mode

Output

Name: Subhrangsu | Type: string
Age: 25 | Type: number
New Age: 26
Graduation Year: null
Enter fullscreen mode Exit fullscreen mode

Summary Checklist

var vs let vs const

Final Thoughts

Variables and data types are the building blocks of JavaScript.

Every program — from simple scripts to large applications —
relies on variables to store and manipulate data.

Key takeaways:

  • Use const by default
  • Use let when values need to change
  • Avoid var in modern JavaScript
  • Understand primitive vs reference types

Once you're comfortable with these concepts,
you can move on to operators, conditions, and functions in JavaScript.

Deepen Your Learning

For the full code examples used in this blog:

  1. Exploring Variables
    variables.js

  2. Mastering Data Types
    datatypes.js

Top comments (0)