JavaScript's type system operates on the principle of dynamic typing, allowing variables to hold different data types throughout their lifecycle without explicit type declarations. The language distinguishes between primitive types, which are immutable and stored by value, and non-primitive types (objects), which are mutable and stored by reference
Primitive Data Types:
String: Text sequences enclosed in single, double, or backtick quotes, supporting Unicode characters and escape sequences
Number: IEEE 754 double-precision floating-point format storing both integers and decimals, with special values like
InfinityandNaNBoolean: Logical values
trueorfalseused in conditional operations and comparisonsNull: Intentional absence of value, representing "nothing" or "empty" (quirk:
typeof nullreturns"object")Undefined: Default state of declared but uninitialized variables, or properties that don't exist
Symbol: Unique, immutable identifiers primarily used as object property keys to avoid naming conflicts
BigInt: Arbitrary precision integers for values exceeding the safe integer limit, denoted with
nsuffix
Non-Primitive Data Types:
Object: Key-value collections including plain objects, arrays, functions, dates, and other complex structures
The typeof operator provides runtime type inspection, returning string representations like "string", "number", or "object" for type identification. JavaScript's automatic type conversion system enables seamless operations between different types, though understanding explicit type checking becomes crucial for robust applications
Examples
// String
let name = "Alice";
let message = `Hello, ${name}!`; // Template literal
// Number
let age = 25;
let price = 19.99;
let scientific = 2e5;
// BigInt
let bigNum = 9007199254740991n;
let bigValue = BigInt(12345678901234567890);
// Boolean
let isActive = true;
let isComplete = false;
// Undefined
let notAssigned;
console.log(notAssigned); // undefined
// Null
let emptyValue = null;
// Symbol
let uniqueId = Symbol('id');
// Object
let person = {
firstName: "Alice",
age: 25
};
// Array (object type)
let colors = ["red", "green", "blue"];
// Function (object type)
function greet() {
return "Hello!";
}
// Type checking
console.log(typeof name); // string
console.log(typeof age); // number
console.log(typeof bigNum); // bigint
console.log(typeof isActive); // boolean
console.log(typeof notAssigned); // undefined
console.log(typeof emptyValue); // object (special case for null)
console.log(typeof uniqueId); // symbol
console.log(typeof person); // object
console.log(Array.isArray(colors));// true
console.log(typeof greet); // function