Skip to content

JavaScript Objects

This lesson explains JavaScript Objects in JavaScript with beginner-friendly examples, practical use cases, and clear best practices.

JavaScript Objects Overview

JavaScript objects are collections of key-value pairs used to store structured data. Objects are one of the most important parts of JavaScript because they allow developers to represent real-world entities such as users, products, orders, settings, API responses, and application state.

An object can contain properties, methods, arrays, nested objects, and functions. JavaScript also provides many built-in Object methods such as Object.keys(), Object.values(), Object.entries(), Object.assign(), and Object.freeze() to work with objects more effectively.

JavaScript Object Example

const user = {
  name: "John",
  age: 30,
  role: "Developer",
  active: true,
  greet: function() {
    return "Hello " + this.name;
  }
};

console.log(user.name);
console.log(user["role"]);
console.log(user.greet());

This example creates a JavaScript object with properties and a method. Object properties can be accessed using dot notation or bracket notation.

Top 10 JavaScript Object Examples

# Example Syntax
1 Create Object const user = { name: "John", age: 30 };
2 Access Property user.name
3 Bracket Notation user["age"]
4 Add Property user.country = "USA";
5 Update Property user.age = 31;
6 Delete Property delete user.age;
7 Object Method const user = { greet: function() { return "Hi"; } };
8 Nested Object const user = { address: { city: "Dallas" } };
9 Loop Object for (const key in user) { console.log(key); }
10 Object Destructuring const { name, age } = user;

JavaScript Object Built-in Methods with Examples and Output

Method Description Example Output
Object.keys() Returns object property names. Object.keys({ name: "John", age: 30 }) ["name", "age"]
Object.values() Returns object property values. Object.values({ name: "John", age: 30 }) ["John", 30]
Object.entries() Returns key-value pairs. Object.entries({ name: "John" }) [["name", "John"]]
Object.fromEntries() Creates object from entries. Object.fromEntries([["name", "John"]]) { name: "John" }
Object.assign() Copies properties into target object. Object.assign({}, { a: 1 }, { b: 2 }) { a: 1, b: 2 }
Object.create() Creates object with prototype. Object.create({ type: "user" }) New object with prototype
Object.freeze() Prevents object changes. Object.freeze({ name: "John" }) Frozen object
Object.seal() Prevents adding or deleting properties. Object.seal({ name: "John" }) Sealed object
Object.isFrozen() Checks whether object is frozen. Object.isFrozen(Object.freeze({})) true
Object.isSealed() Checks whether object is sealed. Object.isSealed(Object.seal({})) true
Object.hasOwn() Checks own property. Object.hasOwn({ name: "John" }, "name") true
hasOwnProperty() Checks property on object. user.hasOwnProperty("name") true
Object.getOwnPropertyNames() Returns all own property names. Object.getOwnPropertyNames({ a: 1 }) ["a"]
Object.getOwnPropertySymbols() Returns symbol properties. Object.getOwnPropertySymbols(obj) [Symbol()]
Object.getPrototypeOf() Returns object prototype. Object.getPrototypeOf(obj) Prototype object
Object.setPrototypeOf() Sets object prototype. Object.setPrototypeOf(obj, proto) Updated object
Object.defineProperty() Defines one property. Object.defineProperty(obj, "id", { value: 1 }) Object with id
Object.defineProperties() Defines multiple properties. Object.defineProperties(obj, { id: { value: 1 } }) Object with properties
Object.getOwnPropertyDescriptor() Returns property descriptor. Object.getOwnPropertyDescriptor(obj, "name") Descriptor object
Object.getOwnPropertyDescriptors() Returns all descriptors. Object.getOwnPropertyDescriptors(obj) Descriptors object
Object.preventExtensions() Prevents adding new properties. Object.preventExtensions({ name: "John" }) Non-extensible object
Object.isExtensible() Checks whether properties can be added. Object.isExtensible({}) true
Object.is() Compares two values. Object.is(NaN, NaN) true
Object.groupBy() Groups items by callback result. Object.groupBy(users, user => user.role) { admin: [...], user: [...] }