TutorialsJavaScriptVariables & Data Types
JavaScript

Variables & Data Types

Variables store data values. In modern JavaScript, use const for values that don't change and let for values that do. Avoid var — it has confusing scoping behavior.

// const — cannot be reassigned (use by default)
const PI = 3.14159;
const siteName = "TechDhaariHub";

// let — can be reassigned
let score = 0;
score = 10;     // ✓ OK
score += 5;     // 15

// var — old way, avoid in modern JS
var legacy = "deprecated";

JavaScript Data Types:

// ── Primitives ──────────────────────────────────

// String
const name = "Arjun";
const city = 'Mumbai';
const msg = `Hello, ${name}! You are from ${city}.`; // template literal
console.log(msg); // "Hello, Arjun! You are from Mumbai."

// Number (integers and decimals share one type)
const age = 25;
const price = 99.99;
const big = 1_000_000; // underscores for readability

// Boolean
const isLoggedIn = true;
const hasError = false;

// null — intentional empty value (you set this)
let currentUser = null;

// undefined — declared but not assigned (JS sets this)
let result;
console.log(result); // undefined

// ── Reference types ─────────────────────────────

// Array — ordered list, zero-indexed
const skills = ["HTML", "CSS", "JavaScript"];
console.log(skills[0]);      // "HTML"
console.log(skills.length);  // 3
skills.push("React");        // add to end
skills.pop();                // remove from end
const [first, ...rest] = skills; // destructuring

// Object — key/value pairs
const person = {
  name: "Priya",
  age: 28,
  isActive: true,
  address: { city: "Bengaluru" },  // nested object
};
console.log(person.name);          // "Priya" (dot notation)
console.log(person["age"]);        // 28 (bracket notation)
console.log(person.address.city);  // "Bengaluru"

const { name: fullName, age: years } = person; // destructuring with rename
💡

Use typeof to check a variable's type: typeof 42 → 'number', typeof 'hi' → 'string', typeof null → 'object' (this is a historical JavaScript bug — null is not actually an object).

Watch & Learn

A recommended video to watch alongside this chapter.

More “Variables & Data Types” videos on YouTube