JavaScript Data Types
JavaScript variables can hold different data types: numbers, strings, objects, booleans, and more. Unlike statically-typed languages, JavaScript has dynamic types, meaning variables can change their type at runtime. Understanding data types is crucial for effective programming.
Primitive Data Types
String is a data type representing text—sequences of characters enclosed in quotes. Strings can use single quotes ('Hello'), double quotes ("Hello"), or backticks (`Hello`). Strings are immutable in JavaScript, meaning once created, their content cannot be changed (though you can create new strings based on existing ones). Strings are used for text, messages, names, labels, and any textual data.
Number is the data type for numeric values, including both integers (whole numbers like 42) and decimals (floating-point numbers like 3.14). JavaScript doesn't distinguish between integers and floats—all numbers are 64-bit floating-point values following the IEEE 754 standard. Numbers can be positive, negative, or special values like Infinity, -Infinity, and NaN (Not a Number).
Boolean represents logical values: true or false. Booleans are the foundation of conditional logic and decision-making in programs. They're returned by comparison operations (5 > 3 is true), used in if statements, and essential for controlling program flow. Any value can be converted to a boolean, with "falsy" values (false, 0, "", null, undefined, NaN) converting to false and all others to true.
Undefined is the type of variables that have been declared but not assigned a value. When you write "let x;" without assigning a value, x is undefined. Functions without a return statement return undefined. Accessing non-existent object properties returns undefined. It represents the absence of an intentional value assignment.
Null represents the intentional absence of a value. Unlike undefined (which means "not yet assigned"), null means "deliberately empty or non-existent." Developers explicitly assign null to variables to indicate "no value." Technically null is a primitive, but typeof null returns "object" due to a long-standing JavaScript quirk.
Symbol, introduced in ES6 (2015), creates unique identifiers that are guaranteed to be different from all other symbols. Even Symbol("description") === Symbol("description") is false—each symbol is unique. Symbols are primarily used as object property keys to avoid naming collisions, especially in library and framework code.
BigInt, introduced in ES2020, represents integers larger than the Number type can safely handle (beyond 2^53 - 1). Create BigInts by appending "n" to an integer (like 1234567890123456789n) or using the BigInt() constructor. BigInts are useful for precise integer calculations with very large numbers, financial calculations, or cryptography.
// String
let name = "John Doe";
let greeting = 'Hello World';
// Number
let age = 25;
let price = 19.99;
// Boolean
let isActive = true;
let hasPermission = false;
// Undefined
let x;
console.log(x); // undefined
// Null
let empty = null;
Complex Data Types
Object is a complex data type that stores collections of key-value pairs (properties). Objects are created with curly braces {} and can contain multiple properties, each with a name (key) and value. Objects are mutable—you can add, modify, or delete properties after creation. Objects are fundamental to JavaScript and used to represent structured data, entities, and complex information.
Array is a special type of object that stores ordered lists of values. Arrays use square brackets [] and contain elements accessed by numeric indices starting at 0. Arrays can hold values of any type, including mixed types, and have a dynamic length that changes as you add or remove elements. Arrays are essential for working with lists, collections, and sequences of data.
Function is technically an object in JavaScript (functions are first-class objects), representing a callable block of reusable code. Functions can be assigned to variables, passed as arguments to other functions, returned from functions, and have properties. Functions are defined with the function keyword or as arrow functions, and they can accept parameters and return values.
The typeof operator is used to determine the data type of a value. It returns a string indicating the type: "string", "number", "boolean", "undefined", "object", "function", or "symbol". Note that typeof null returns "object" (a known quirk), typeof arrays returns "object" (use Array.isArray() to check for arrays), and typeof functions returns "function".
JavaScript is dynamically typed, meaning variables don't have fixed types—they can change type during program execution. A variable can hold a number, then be reassigned to hold a string, then an object. The type is determined by the current value, not the variable declaration. This flexibility is powerful but requires careful attention to avoid type-related bugs. TypeScript adds optional static typing to JavaScript for developers who prefer type safety.
// Object
let person = {
name: "John",
age: 30,
city: "New York"
};
// Array
let colors = ["red", "green", "blue"];
let numbers = [1, 2, 3, 4, 5];
// Function
function greet(name) {
return "Hello, " + name;
}
// typeof operator
console.log(typeof "Hello"); // "string"
console.log(typeof 42); // "number"
console.log(typeof true); // "boolean"
console.log(typeof {}); // "object"
console.log(typeof []); // "object"
console.log(typeof null); // "object" (known quirk)