logo

Variables and Data Types

Learn about JavaScript variables and data types

Variables and Data Types

Variables are containers for storing data values in JavaScript.

Variable Declarations

// let - block-scoped, can be reassigned
let name = "John";
name = "Jane"; // ✅ Works

// const - block-scoped, cannot be reassigned
const age = 25;
// age = 26; // ❌ Error

// var - function-scoped (avoid in modern JS)
var isStudent = true;

Data Types

Primitive Types

// String
let message = "Hello World";
let template = `Hello ${name}`;

// Number
let integer = 42;
let decimal = 3.14;
let negative = -10;

// Boolean
let isActive = true;
let isComplete = false;

// Undefined
let notDefined;
console.log(notDefined); // undefined

// Null
let empty = null;

// Symbol (ES6)
let symbol = Symbol('id');

Reference Types

// Object
let person = {
  name: "Alice",
  age: 30,
  city: "New York"
};

// Array
let numbers = [1, 2, 3, 4, 5];
let mixed = ["text", 42, true, null];

// Function
function greet(name) {
  return `Hello, ${name}!`;
}

Type Checking

console.log(typeof "Hello"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"
console.log(typeof undefined); // "undefined"
console.log(typeof null); // "object" (known quirk)
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof function(){}); // "function"

Understanding variables and data types is fundamental to JavaScript programming!