Secure & Private (Zero Data Retention)
Free Access • No Sign-Up
Variables & Data Types
Variables are the building blocks of any programming language. They allow you to store data, update it, and reference it by name throughout your code.
Declaring Variables
In modern JavaScript, you should almost always use let and const. const is for values that won't be reassigned, while let is for values that will change.
Try It Yourself
Data Types
JavaScript has several basic data types: Strings (text), Numbers (integers and decimals), Booleans (true/false), Null (intentional absence of value), and Undefined (variable declared but not assigned).
Try It Yourself
📋 Copy JavaScript Variables & Data Types Cheat Sheet
Trap #1: Temporal Dead Zone (TDZ) with let and const
Accessing a let or const variable before its declaration throws a fatal ReferenceError: Cannot access variable before initialization. Unlike var, variables are hoisted into the TDZ without an initial value.
Trap #2: var Function Scoping & Bleed Trap
Variables declared with var are function-scoped or globally-scoped, completely ignoring if blocks and for loops. This leaks variables outside loops and causes unintended mutations.
Trap #3: typeof null === "object" Historical Bug
Due to a legacy type tag quirk from JavaScript's first version in 1995, typeof null returns "object". Always check val === null explicitly.
Trap #4: const Object Mutation Misconception
const freezes the variable binding, NOT the underlying object. Writing const user = {}; user.name = "Neo"; is completely legal. To make an object immutable, use Object.freeze(user).
Trap #5: Silent Global Creation in Non-Strict Mode
Assigning to an undeclared variable (score = 100) in sloppy mode automatically creates a property on the global window object. Always write "use strict"; or use ES modules.
💬 Frequently Asked Questions
What is the practical difference between let and const in modern JavaScript?
Use const by default for all variables that will not be reassigned. Use let only when a variable needs to be rebound to a new value (such as loop counters or state accumulators).
What are the 7 primitive data types in JavaScript?
The 7 primitives are string, number, bigint, boolean, undefined, symbol, and null. All other data structures (functions, arrays, objects) are reference objects.
How does variable hoisting work in JavaScript?
During the compilation phase, variable and function declarations are moved to the top of their containing scope. var declarations are initialized to undefined, while let and const enter the Temporal Dead Zone.
Why does Symbol exist in JavaScript?
Symbol creates unique, collision-proof identifiers often used as hidden object property keys that will not conflict with normal string keys.
What is the difference between null and undefined?
undefined indicates a variable has been declared but has not yet been assigned any value. null is an intentional assignment representing the explicit absence of any object value.