Understanding Variables and Data Types Fundamentals in JavaScript

Published: (February 22, 2026 at 06:07 AM EST)
6 min read
Source: Dev.to

Source: Dev.to

What Is a Variable?

Think of a variable as a box:

  • You put your name inside it.
  • The box’s only responsibility is to 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. If you don’t understand variables, you’re not really programming — you’re just typing garbage.

Key reasons to use variables

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

Simple Example

let name = "Ritam";
console.log(name);
  • name → variable
  • "Ritam" → value stored inside it

Think of it like:

[ name ]  →  "Ritam"

Without variables, programs would be rigid and useless.

Declaring Variables in JavaScript

JavaScript gives us three ways to declare variables:

DeclarationIntroducedTypical Use
varES1 (1997)Legacy code, function‑scoped
letES6 (2015)Block‑scoped, mutable
constES6 (2015)Block‑scoped, immutable

var

var age = 20;
  • The oldest and original way to declare variables.
  • Function‑scoped (or globally scoped if declared outside a function).
  • Can be re‑declared and re‑assigned, which often leads to bugs.

Tip: In modern code you’ll rarely use var. Prefer let or const.

let

let city = "Kolkata";
  • Block‑scoped.
  • Can be re‑assigned but not re‑declared in the same scope.
let score = 10;
score = 20; // ✅ allowed

const

const country = "India";
  • Block‑scoped.
  • Cannot be re‑assigned after its initial definition.
const PI = 3.14;
PI = 3.14159; // ❌ Error
console.log(PI); // 3.14

When to use const – whenever a value should stay constant (e.g., configuration values, fixed references).

For a deeper dive into why let and const were introduced, see my previous blog [here].

Data Types

Data types define the kind of value stored inside a variable. JavaScript has two broad categories:

  1. Primitive Data Types
  2. Non‑Primitive (Reference) Data Types

In this post we’ll cover the primitive types. Future posts will dive into objects, arrays, functions, etc.

⚠️ Note: This is a basic overview. For a complete reference, explore the MDN Web Docs.

Primitive Types Overview

TypeDescriptionExample
StringTextual data, wrapped in quotes ("" or '').let name = "Ritam";
NumberNumeric data (both integers and floating‑point).let age = 21;
BooleanLogical true/false.let isStudent = true;
nullIntentional absence of any value.let result = null;
undefinedVariable declared but not assigned a value.let data; console.log(data); // undefined
BigIntVery large integers beyond the safe‑integer limit.let big = 12345678901234567890n;
SymbolUnique identifiers.const id = Symbol("rune_of_fire");

String

let name = "Ritam";
let programming = "JavaScript";
let id = "123"; // still a string!

Number

let age = 21;
let price = 99.99;

Boolean

let isStudent = true;
let isLoggedIn = false;

null

let result = null;

undefined

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

Caution: Although you can explicitly assign undefined to a variable, it’s generally discouraged because it can make debugging harder.

BigInt

let bigNumber = 123456789012345678901234567890n; // trailing "n"
let bigValue = BigInt(12345678901234567890);       // using constructor

When might you need BigInt?

  • Financial systems (precise currency calculations)
  • Cryptography
  • Scientific calculations
  • Large database IDs

Symbol

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

console.log(uniqueRuneId1 === uniqueRuneId2); // false

Typical uses

  • Unique object property keys
  • Advanced JavaScript patterns (e.g., iterators)
  • Built‑in mechanisms (e.g., Symbol.iterator)

The typeof Operator

console.log(typeof "Ritam"); // "string"
console.log(typeof 21);      // "number"
console.log(typeof true);    // "boolean"

⚡ Small assignment: What does typeof null return? Think about it before you run the code!

Understanding typeof deeply is important because its results aren’t always what you expect (e.g., typeof null is "object"). We’ll explore this operator in more detail in future posts.

Quick Comparison: var vs let vs const

Featurevarletconst
ScopeFunction‑scoped or globalBlock‑scopedBlock‑scoped
HoistingYes (initialized as undefined)Yes (temporal dead zone)Yes (temporal dead zone)
Re‑declarationAllowed within same scopeNot allowed in same blockNot allowed in same block
Re‑assignmentAllowedAllowedNot allowed after initialization
Use caseLegacy code, rarely recommendedVariables that change valueConstants, configuration values

Variable Declarations in JavaScript

Featurevarletconst
Signed?✅ Yes✅ Yes❌ No
Scope typeFunctionBlockBlock
HoistingYesYes – but the Temporal Dead Zone (TDZ) appliesYes – but the Temporal Dead Zone (TDZ) applies
Modern usage?❌ Avoid✅ Yes✅ Yes

Practical Rule

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

Why Variables Matter

Variables aren’t just a beginner topic – they’re 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 Variables Gives You Real Control

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

…and this knowledge translates into solid, maintainable code.

Getting Started

  1. Start simple.
  2. Build understanding step‑by‑step.

That’s how real comprehension happens.

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

0 views
Back to Blog

Related posts

Read more »