Learn JavaScript Variables, Data Types & Scope

Variables, Data Types & Scope

⏱ 15 min read
JavaScript has three ways to declare variables: var (old — avoid it), let (reassignable), and const (constant — use this by default).

Always prefer const. Use let when you need to reassign. Never use var in modern code.

The 7 Primitive Types:

number — integers AND decimals (no separate int/float like other languages)
Example: let age = 25; let price = 9.99;

string — text in single, double, or backtick quotes
Example: let name = 'Alice';

boolean — true or false (lowercase)
Example: let isLoggedIn = true;

null — intentional absence of a value (you set this deliberately)
undefined — variable declared but not assigned (JavaScript sets this automatically)

symbol — unique identifier (advanced, rarely used directly)
bigint — very large integers: 9007199254740993n

typeof Operator:
typeof 42 -> "number"
typeof "hello" -> "string"
typeof true -> "boolean"
typeof undefined -> "undefined"
typeof null -> "object" <- famous JS bug! null is NOT an object.
typeof {} -> "object"
typeof [] -> "object" <- arrays are objects too!

Template Literals (backtick strings):
const msg = `Hello, ${name}! You are ${age} years old.`
Supports multi-line strings and embedded expressions — far better than concatenation.

Type Coercion — DANGER:
JavaScript automatically converts types, causing surprising results:
"5" + 3 -> "53" (string concatenation!)
"5" - 3 -> 2 (numeric subtraction)
"5" == 5 -> true (loose equality — converts types)
"5" === 5 -> false (strict equality — no conversion)
ALWAYS use === (triple equals) to compare values. Never use ==.
Code Example
// Variable declaration
const PI        = 3.14159;   // constant — cannot reassign
let   score     = 100;       // can reassign
let   name      = "Alice";
let   isStudent = true;

// let allows reassignment
score = 200;

// const does NOT allow reassignment
// PI = 3;  // TypeError!

// 7 primitive types
console.log(typeof 42);               // "number"
console.log(typeof 3.14);             // "number" — same type!
console.log(typeof "hello");          // "string"
console.log(typeof true);             // "boolean"
console.log(typeof undefined);        // "undefined"
console.log(typeof null);             // "object" — JS quirk!
console.log(typeof 9007199254740993n);// "bigint"

// Template literals
const city = "Lagos";
console.log(`${name} lives in ${city}.`);
console.log(`Score doubled: ${score * 2}`);   // expressions work!

// ALWAYS use === not ==
console.log("5" == 5);    // true  — DANGEROUS
console.log("5" === 5);   // false — CORRECT
console.log(null == undefined);   // true
console.log(null === undefined);  // false

// Block scoping — let/const are block-scoped
{
  let blockVar = 'I am inside a block';
  console.log(blockVar);   // works here
}
// console.log(blockVar);  // ReferenceError — out of scope!

// var is function-scoped (NOT block-scoped — this is why we avoid it)
{
  var oldVar = 'I leak out of blocks!';
}
console.log(oldVar);   // "I leak out of blocks!" — BAD behaviour
← Hello, JavaScript! — Your First Progra Operators, Conditionals & Type Coercion →

Log in to track your progress and earn badges as you complete lessons.

Log In to Track Progress