Data Types

JavaScript has 8 primitive data types and objects:

Type Checking

console.log(typeof undefined);   // "undefined"
console.log(typeof null);        // "object" (Legacy bug in JavaScript)
console.log(typeof true);        // "boolean"
console.log(typeof 42);          // "number"
console.log(typeof 9007199254740991n); // "bigint"
console.log(typeof "Hello");     // "string"
console.log(typeof Symbol("id")); // "symbol"

console.log(typeof {});          // "object" (Plain object)
console.log(typeof []);          // "object" (Array is a special kind of object)
console.log(typeof function(){}); // "function" (Function is a special type of object)
console.log(typeof class {});    // "function" (Classes are syntactic sugar for functions)
console.log(typeof new Date());  // "object" (Date is a built-in object)
console.log(typeof /regex/);     // "object" (RegExp is an object)
console.log(typeof Math);        // "object" (Math is a built-in object)
console.log(typeof JSON);        // "object" (JSON is also an object)
console.log(typeof NaN);         // "number" (NaN stands for "Not-a-Number" but is still of type number)
console.log(typeof Infinity);    // "number" (Infinity is a special numeric value)
console.log(typeof (() => {}));  // "function" (Arrow function)
console.log(typeof new Map());   // "object" (Map is an object)
console.log(typeof new Set());   // "object" (Set is an object)
console.log(typeof new WeakMap()); // "object" (WeakMap is an object)
console.log(typeof new WeakSet()); // "object" (WeakSet is an object)

Variable Declarations & Scope

JavaScript supports three variable declarations:

let x = 10;   // Block-scoped

const PI = 3.14;  // Cannot be reassigned
const obj = { a: 1 };
obj.a = 2;  // Allowed (const prevents reassignment, but object properties are mutable)

var y = 20;   // Function-scoped (not recommended)

Scope