JavaScript is an object‑centric language. Arrays, functions, dates, errors, even Math and JSON are all objects or work closely with objects. Before you dive into prototypes or classes, it’s worth getting very comfortable with what an object actually is.
At its core, an object is a collection of key–value pairs. Keys are usually strings (or symbols), and values can be anything: numbers, strings, booleans, arrays, functions, or other objects.
// A simple object representing a user profileconst user = { name: 'Ada Lovelace', age: 36, isAdmin: true, skills: ['mathematics', 'programming'], address: { city: 'London', country: 'UK', }, greet() { // `this` refers to the current object console.log(`Hello, I'm ${this.name}.`); },};
console.log(user.name); // "Ada Lovelace"console.log(user.skills[0]); // "mathematics"console.log(user.address.city); // "London"user.greet(); // "Hello, I'm Ada Lovelace."Objects are the default way to represent structured data in JavaScript—configuration, user profiles, HTTP responses, options for a function, and more.
// A configuration object for an imaginary appconst appConfig = { apiBaseUrl: 'https://api.example.com', retryCount: 3, featureFlags: { enableBeta: true, enableLogging: false, },};
// You can read nested configuration values easilyconsole.log(appConfig.retryCount); // 3console.log(appConfig.featureFlags.enableBeta); // trueObjects can also hold behavior through methods (functions stored as properties).
// A cart item with both data and behaviorconst cartItem = { id: 1, name: 'Mechanical Keyboard', price: 120, quantity: 1, // Method to compute total price for this item total() { return this.price * this.quantity; },};
console.log(cartItem.total()); // 120
cartItem.quantity = 3; // Update dataconsole.log(cartItem.total()); // 360 (method uses updated state)You can even use objects to group related utility functions under a single namespace‑like value.
// A simple "namespace" of utilitiesconst mathUtils = { // Add two numbers add(a, b) { return a + b; }, // Clamp a value between min and max clamp(value, min, max) { return Math.min(Math.max(value, min), max); },};
console.log(mathUtils.add(2, 3)); // 5console.log(mathUtils.clamp(10, 0, 5)); // 5Because objects are so flexible, you’ll see them used as maps/dictionaries, as configuration containers, as data models, and as small state machines. Getting comfortable reading and writing object literals is the foundation for everything else in JavaScript’s object system.