Back to course

Introduction to Objects: Key-Value Pairs

JavaScript: The Complete '0 to Hero' Beginner Course

62. Introduction to Objects

Objects are the foundation of complex data structures in JavaScript. Unlike arrays, which use numbered indices, objects store data as properties in unordered key-value pairs.

Defining Objects (Object Literals)

Objects are created using curly braces {}.

javascript const user = { // key: value, firstName: 'Jane', lastName: 'Doe', age: 30, isLoggedIn: true, 'favorite color': 'blue' // Keys can be strings with spaces };

const emptyObject = {};

Properties and Methods

  • A property holds a piece of data (e.g., age: 30).
  • A method is a property whose value is a function (used to perform an action related to the object, covered soon).