DEV Community

Cover image for Understanding Variables and Data Types Fundamentals in JavaScript
Ritam Saha
Ritam Saha

Posted on

Understanding Variables and Data Types Fundamentals in JavaScript

Introduction: “Wait… Where Is My Data Coming From?”

Imagine this.

You’re filling out a form on a website.

You type your name.
You enter your age.
You tick a checkbox that says “I am a student.”

You click submit.

Now pause for a second.

Where did that information go?

How does the website “remember” what you typed?
How does it print your name back on the screen?
How does it decide whether to show you student discounts?

Behind the scenes, your data is being stored somewhere temporarily while the program works with it.

That “somewhere” is what we call a variable.

But thinking of it as a technical concept can feel abstract at first. So let’s simplify it.

Imagine you have a box.

You put your name inside it.
Later, you replace it with your age.
Then maybe you store whether you’re a student or not.

That box has one responsibility: hold information so you can use it later.

In JavaScript, that box is called a variable.

Every program — whether it’s a simple calculator or a large-scale application like Netflix — depends on variables to store and manage data.

And if you don’t understand variables, you’re not really programming — you’re just typing garbage. Understanding variables and data types are the most fundamental concept in any programming language.

So let’s build this from the ground up.


What Are Variables and Why Do We Need Them?

A variable is a named container used to store data.
Instead of hard-coding values everytime, we store them in variables so that we can:

  • Reuse values
  • Update values
  • Make programs dynamic
  • Write cleaner code

Example

let name = "Ritam";
console.log(name);
Enter fullscreen mode Exit fullscreen mode

Here:

  • name → variable
  • "Ritam" → value stored inside it

Think of it like:

[ name ]  →  "Ritam"
Enter fullscreen mode Exit fullscreen mode

Without variables, programs would be rigid and useless.


How to Declare Variables in JavaScript

JavaScript gives us three ways to declare variables:

  • var
  • let
  • const

Let’s understand them one by one.

Using var

var age = 20;
Enter fullscreen mode Exit fullscreen mode

var is the oldest and original way to declare variables in JavaScript.
However, due to some Hoisting and Scoping related issues, var is not that much used in real world applications. Though it's not obsolete, but it's not in that much of use. I have discussed about these problem in detail in my previous blog. Check it out here after reading the current one!

Using let & const

With the introduction of ES6 (ECMAScript 2015), two new variable declarations, let and const, were introduced. These declarations provide more predictable and safer behavior compared to var.

Using let:

let city = "Kolkata";
Enter fullscreen mode Exit fullscreen mode

let allows you to:

  • Declare variables
  • Change their values later

Example:

let score = 10;
score = 20;   // Allowed
Enter fullscreen mode Exit fullscreen mode

Using const:

const country = "India";
Enter fullscreen mode Exit fullscreen mode

const means the value cannot be reassigned. We have to use const when the value shouldn't be changed.
Example:

const PI = 3.14;
PI = 3.14159;   // ❌ Error
console.log(PI); // Output: 3.14
Enter fullscreen mode Exit fullscreen mode

In my previous blog, I have already discussed about the problem with var, why let & const were introduced and how these solve the previous problems. Please check it out here.


Primitive Data Types in JavaScript

Data types define the kind of value stored inside a variable. There are mainly two data types in JavaScript -

  • Primitive Data Types
  • Non-Primitive Data Types

In this blog we are going to discuss a little about the primitive data type. In the upcoming blogs, we will deep dive into the non-primitive data types.

‼️ In this blog, I am covering very basics. It's not possible to explore every properties in a single blog. So I suggest you to explore docs (I suggest MDN Web Docs) to get to know more about these primitive data types, their properties & in-built functions. You will learn more and better by exploring by yourself, everything will not be spoon-fed!

string

This data type is used to store textual data i.e, string. Generally it's denoted by keeping the value with "" or ''.
Example:

let name = "Ritam";
let programming = "JavaScript"
let id = "123" (still a string!)
Enter fullscreen mode Exit fullscreen mode

number

This data type is used to store numeric values.

let age = 21;
let price = 99.99;
Enter fullscreen mode Exit fullscreen mode

JavaScript does not separate integers and decimals — both are number.

boolean

This data type represents true or false.

let isStudent = true;
let isLoggedIn = false;
Enter fullscreen mode Exit fullscreen mode

Real-life analogy:
Are you logged in?
Yes → true
No → false

null

This data type represents the intentional absence of value. i.e, explicitly denotion of standalone empty value.

let result = null;
Enter fullscreen mode Exit fullscreen mode

It means:
“There is no value here — on purpose.”

undefined

When a variable is declared but not assigned a value. It's like an empty placeholder.

let data;
console.log(data); // undefined
Enter fullscreen mode Exit fullscreen mode

It means:
“No value has been assigned yet.”

Though it's also possible intentionally assigning undefined to any variable, but if you ask me, then I won't recommend it, it doesn't make any sense at all assigning that way!

bigInt

This data type is used to store very large integers that are generally bigger than the normal number limit.
JavaScript numbers have a maximum safe limit:
9007199254740991 (2^53 - 1)

If you go beyond that, precision problems start.
That’s where BigInt comes in.

let bigNumber = 123456789012345678901234567890n;
Enter fullscreen mode Exit fullscreen mode

Notice the n at the end — that makes it a BigInt.

You can also create it like this:

let bigValue = BigInt(12345678901234567890); //using constructor
Enter fullscreen mode Exit fullscreen mode

There's no strict rule that we can only define bigInt for the out of range numbers i.e, 123n or BigInt(20) is also valid bigInt.

But when would we need this?

  • Financial systems
  • Cryptography
  • Scientific calculations
  • Large database IDs

For beginners, you won’t use it daily — but it’s important to know it exists.

symbol

This data type is used to have uniqueness. A Symbol will create unique value that will never clash with another value. Further can be understood by the help of an example.

const uniqueRuneId1 = Symbol("rune_of_fire");
const uniqueRuneId2 = Symbol("rune_of_fire");

console.log(uniqueRuneId1 === uniqueRuneId2) //false
Enter fullscreen mode Exit fullscreen mode

Even though both have the same description "rune_of_fire", they are not equal.

Why? Because every Symbol is unique.

But the question is, where is it useful?

Symbols are mainly used:

  • To create unique object property keys
  • Inside advanced JavaScript patterns
  • In built-in JavaScript mechanisms

How Do We Check a Variable’s Data Type?

JavaScript provides a built-in operator called typeof that tells you the type of a value.

Example:

console.log(typeof "Ritam"); // string
console.log(typeof 21); // number
console.log(typeof true); // boolean
Enter fullscreen mode Exit fullscreen mode

You can try it yourself in the browser console or in your IDE.

⚠️ Small assignment for you:
What do you think typeof null returns? just think about it first before printing it in console.
And what about typeof undefined?
Try it out before searching.

Understanding typeof deeply is very important — because not everything it returns is as obvious as it looks. We’ll explore this operator in more detail in the future blogs — for now, just know that it exists and start experimenting with it.


Basic Difference Between var, let, and const

Refer my previous blog for more insights. Here’s a simple comparison given in tabular format:

Feature var let const
Can be reassigned? ✅ Yes ✅ Yes ❌ No
Scope type Function Block Block
Hoisting Yes Yes but concept of TDZ involved Yes but concept of TDZ involved
Modern usage? ❌ Avoid ✅ Yes ✅ Yes

Practical Rule

  • Use const by default.
  • Use let when value needs to change.
  • Avoid var in modern JavaScript.

Conclusion: The Foundation of Everything in JavaScript

Variables are not just a beginner topic.

They are the foundation of:

  • Functions
  • Loops
  • APIs
  • Frameworks like React
  • Backend development with Node.js

If you misunderstand variables and data types, everything built on top becomes unstable.

Mastering:

  • How to declare variables
  • When to use let vs const
  • What data types represent

…gives you real control over JavaScript.

Start simple.
Read docs.
Write small programs.
Experiment in the console.
Break things intentionally.
Observe behaviour.

That’s how real understanding happens.


Follow for more beginner-friendly breakdowns of core software engineering concepts.

Top comments (0)