Understanding Variables and Data Types Fundamentals in JavaScript
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:
| Declaration | Introduced | Typical Use |
|---|---|---|
var | ES1 (1997) | Legacy code, function‑scoped |
let | ES6 (2015) | Block‑scoped, mutable |
const | ES6 (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. Preferletorconst.
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:
- Primitive Data Types
- 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
| Type | Description | Example |
|---|---|---|
String | Textual data, wrapped in quotes ("" or ''). | let name = "Ritam"; |
Number | Numeric data (both integers and floating‑point). | let age = 21; |
Boolean | Logical true/false. | let isStudent = true; |
null | Intentional absence of any value. | let result = null; |
undefined | Variable declared but not assigned a value. | let data; console.log(data); // undefined |
BigInt | Very large integers beyond the safe‑integer limit. | let big = 12345678901234567890n; |
Symbol | Unique 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
undefinedto 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 nullreturn? 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
| Feature | var | let | const |
|---|---|---|---|
| Scope | Function‑scoped or global | Block‑scoped | Block‑scoped |
| Hoisting | Yes (initialized as undefined) | Yes (temporal dead zone) | Yes (temporal dead zone) |
| Re‑declaration | Allowed within same scope | Not allowed in same block | Not allowed in same block |
| Re‑assignment | Allowed | Allowed | Not allowed after initialization |
| Use case | Legacy code, rarely recommended | Variables that change value | Constants, configuration values |
Variable Declarations in JavaScript
| Feature | var | let | const |
|---|---|---|---|
| Signed? | ✅ Yes | ✅ Yes | ❌ No |
| Scope type | Function | Block | Block |
| Hoisting | Yes | Yes – but the Temporal Dead Zone (TDZ) applies | Yes – but the Temporal Dead Zone (TDZ) applies |
| Modern usage? | ❌ Avoid | ✅ Yes | ✅ Yes |
Practical Rule
- Use
constby default. - Use
letwhen the value needs to change. - Avoid
varin 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
letvsconst - What data types represent
…and this knowledge translates into solid, maintainable code.
Getting Started
- Start simple.
- Build understanding step‑by‑step.
That’s how real comprehension happens.
Follow for more beginner‑friendly breakdowns of core software‑engineering concepts.