📦 JavaScript Object
Methods: A Complete Guide
Working with objects is at the heart of JavaScript programming.
Whether you are building classes, handling API responses, or manipulating data structures, knowing the native Object
methods will make your code more powerful, secure, and expressive.
In this article, we will explore the most important Object
methods available in JavaScript, see examples in action, and understand when to use each of them.
🔗 Reference: MDN Web Docs – Object
🔑 Why Object
Methods Matter
JavaScript objects are dynamic, meaning you can create, extend, freeze, or inspect them at runtime.
Native Object
methods provide a safe and standard way to handle these operations without reinventing the wheel.
📦 Complete List of Object
Methods
🔤 Method | 📌 Description | 💻 Example | 🔁 Use Cases |
---|---|---|---|
Object.keys(obj) |
Returns an array of keys (enumerable own properties). | Object.keys({a:1,b:2}) // ["a","b"] |
Iterations, debugging |
Object.values(obj) |
Returns an array of values. | Object.values({a:1,b:2}) // [1,2] |
Stats, aggregations |
Object.entries(obj) |
Returns array of [key,value] pairs. |
Object.entries({a:1}) // [["a",1]] |
Mapping, converting to Map
|
Object.fromEntries(arr) |
Creates an object from key-value pairs. | Object.fromEntries([["a",1]]) // {a:1} |
Transforming results of map , filter
|
Object.assign(dest, ...src) |
Copies properties from sources into destination. | Object.assign({}, {a:1},{b:2}) |
Cloning, merging configs |
Object.create(proto, props?) |
Creates a new object with the given prototype. | const o = Object.create(null) |
Prototype patterns, inheritance |
Object.defineProperty(obj, prop, desc) |
Defines a single property with fine control. | Object.defineProperty(user,"id",{value:1, writable:false}) |
Permissions, library design |
Object.defineProperties(obj, props) |
Defines multiple properties at once. | Object.defineProperties(user,{id:{value:1},name:{value:"Ana"}}) |
Advanced configuration |
Object.getOwnPropertyDescriptor(obj, prop) |
Gets the descriptor of a property. | Object.getOwnPropertyDescriptor(user,"id") |
Debugging, validation |
Object.getOwnPropertyDescriptors(obj) |
Gets descriptors for all properties. | Object.getOwnPropertyDescriptors(user) |
Deep cloning with descriptors |
Object.getOwnPropertyNames(obj) |
Returns all own property names (even non-enumerable). | Object.getOwnPropertyNames(Math) |
Internal inspection |
Object.getOwnPropertySymbols(obj) |
Returns all symbol properties. | Object.getOwnPropertySymbols(obj) |
Metaprogramming |
Object.getPrototypeOf(obj) |
Gets the prototype of an object. | Object.getPrototypeOf(new Date()) |
Class inspection, OOP |
Object.setPrototypeOf(obj, proto) |
Sets a new prototype for an object. | Object.setPrototypeOf(obj, Array.prototype) |
Advanced patterns |
Object.is(val1, val2) |
Compares values like === but handles NaN and +0/-0 . |
Object.is(NaN,NaN) // true |
Safe validations |
Object.isExtensible(obj) |
Checks if new properties can be added. | Object.isExtensible(obj) |
Object security |
Object.preventExtensions(obj) |
Prevents adding new properties. | Object.preventExtensions(obj) |
Lock down structures |
Object.seal(obj) |
Prevents adding/removing properties (but allows value changes). | Object.seal(user) |
Fixed object structures |
Object.isSealed(obj) |
Checks if an object is sealed. | Object.isSealed(obj) |
Debug, validations |
Object.freeze(obj) |
Freezes object (no changes allowed). | Object.freeze(user) |
Config objects, constants |
Object.isFrozen(obj) |
Checks if object is frozen. | Object.isFrozen(obj) |
Debug, immutability |
🧑💻 Practical Example
Here’s a short script based on our Persona
class, showcasing several methods in action:
// Define a simple class
class Persona {
constructor(nombre, edad, dni) {
this.nombre = nombre;
this.edad = edad;
this.dni = dni;
}
}
const persona1 = new Persona("Anthony", 25, "12345678");
console.log("========== BASIC INSPECTION ==========");
// Object.keys -> returns own enumerable property names
console.log(Object.keys(persona1));
// ["nombre", "edad", "dni"]
// Object.values -> returns values of enumerable properties
console.log(Object.values(persona1));
// ["Anthony", 25, "12345678"]
// Object.entries -> returns [key, value] pairs
console.log(Object.entries(persona1));
// [["nombre","Anthony"],["edad",25],["dni","12345678"]]
// Object.fromEntries -> creates object from entries
const entries = [["name", "Alice"], ["age", 30]];
console.log(Object.fromEntries(entries));
// { name: "Alice", age: 30 }
console.log("\n========== CREATION & COPYING ==========");
// Object.assign -> copy properties into a new object
const copy = Object.assign({}, persona1);
console.log(copy);
// { nombre: 'Anthony', edad: 25, dni: '12345678' }
// Object.create -> create object with prototype
const proto = { greet() { return "Hello from prototype!"; } };
const objWithProto = Object.create(proto);
console.log(objWithProto.greet());
// "Hello from prototype!"
console.log("\n========== PROPERTY DEFINITIONS ==========");
// Object.defineProperty -> define single property with descriptor
const user = {};
Object.defineProperty(user, "id", { value: 1, writable: false });
console.log(user.id); // 1
user.id = 2; // ignored because writable: false
console.log(user.id); // still 1
// Object.defineProperties -> define multiple properties
Object.defineProperties(user, {
name: { value: "John", writable: true },
role: { value: "Admin", writable: false }
});
console.log(user); // { id: 1, name: "John", role: "Admin" }
// Object.getOwnPropertyDescriptor -> inspect one property
console.log(Object.getOwnPropertyDescriptor(user, "id"));
// { value: 1, writable: false, enumerable: false, configurable: false }
// Object.getOwnPropertyDescriptors -> inspect all
console.log(Object.getOwnPropertyDescriptors(user));
// Object.getOwnPropertyNames -> get all property names (including non-enumerable)
console.log(Object.getOwnPropertyNames(user));
// [ 'id', 'name', 'role' ]
// Object.getOwnPropertySymbols -> get symbol properties
const sym = Symbol("secret");
user[sym] = "hidden";
console.log(Object.getOwnPropertySymbols(user));
// [ Symbol(secret) ]
console.log("\n========== PROTOTYPES ==========");
// Object.getPrototypeOf -> get prototype of an object
console.log(Object.getPrototypeOf(persona1));
// Persona {}
// Object.setPrototypeOf -> set a new prototype
const newProto = { salute() { return "Hey!"; } };
Object.setPrototypeOf(persona1, newProto);
console.log(persona1.salute()); // "Hey!"
console.log("\n========== COMPARISONS ==========");
// Object.is -> strict comparison (handles NaN and +0/-0)
console.log(Object.is(NaN, NaN)); // true
console.log(Object.is(0, -0)); // false
console.log(Object.is(25, 25)); // true
console.log("\n========== EXTENSIBILITY & SEALING ==========");
// Object.isExtensible -> check if we can add properties
console.log(Object.isExtensible(persona1)); // true
// Object.preventExtensions -> block adding new properties
Object.preventExtensions(persona1);
persona1.newProp = "test"; // ignored
console.log(persona1.newProp); // undefined
// Object.seal -> block adding/removing properties, allow changes
Object.seal(persona1);
persona1.nombre = "Lucas"; // allowed
delete persona1.edad; // ignored
console.log(persona1); // { nombre: "Lucas", edad: 25, dni: "12345678" }
// Object.isSealed -> check if sealed
console.log(Object.isSealed(persona1)); // true
// Object.freeze -> block all modifications
Object.freeze(persona1);
persona1.edad = 99; // ignored
console.log(persona1.edad); // 25
// Object.isFrozen -> check if frozen
console.log(Object.isFrozen(persona1)); // true
📌 Conclusion
Mastering Object methods is crucial for every JavaScript developer.
They let you inspect, clone, protect, and extend objects safely.
By combining these methods with classes and object-oriented programming, you gain more control over your code and data structures.
If you’re diving deeper into OOP or functional programming, these tools will quickly become indispensable.
📚 Reference
- Mozilla Developer Network. (n.d.). Object - JavaScript | MDN. Retrieved August 2025, from https://developer.mozilla.org/es/docs/Web/JavaScript/Reference/Global_Objects/Object
Top comments (0)