JavaScript provides several built-in object methods that help you work with objects efficiently. These methods allow you to retrieve property names, access property values, convert properties into key-value pairs, copy properties between objects, create objects with a specified prototype, and control whether objects can be modified.
In this tutorial, you will learn seven important JavaScript object methods: Object.keys(), Object.values(), Object.entries(), Object.assign(), Object.create(), Object.freeze(), and Object.seal().
What Are JavaScript Object Methods?
JavaScript object methods are built-in functions that perform operations on objects. Many commonly used object utilities are called through the built-in Object constructor.
For example, Object.keys() returns an array containing an object’s own enumerable property names.
const student = {
name: "Rahul",
age: 21,
course: "JavaScript"
};
console.log(Object.keys(student));
Output:
["name", "age", "course"]
Here, Object.keys() returns the names of the properties in the student object.
If you are new to objects, first learn how to create JavaScript objects and how nested objects organize related information.
Object.keys() in JavaScript
The Object.keys() method returns an array containing the names of an object’s own enumerable string-keyed properties.
Syntax:
Object.keys(object);
Example:
const employee = {
name: "Anjali",
age: 25,
department: "IT"
};
const keys = Object.keys(employee);
console.log(keys);
Output:
["name", "age", "department"]
The returned array contains the property names name, age, and department.
Using Object.keys() to Count Properties
You can use the length property of the returned array to count an object’s enumerable properties.
const product = {
name: "Laptop",
price: 55000,
brand: "Dell"
};
const keys = Object.keys(product);
console.log(keys.length);
Output:
3
This approach is useful when you need to determine how many enumerable properties an object contains.
Using Object.keys() in a Loop
You can combine Object.keys() with forEach() to process each property name.
const student = {
name: "Rahul",
age: 21,
course: "JavaScript"
};
Object.keys(student).forEach(function(key) {
console.log(key);
});
Output:
name
age
course
You can then use each key to retrieve its corresponding value.
Object.keys(student).forEach(function(key) {
console.log(key + ": " + student[key]);
});
Output:
name: Rahul
age: 21
course: JavaScript
Object.values() in JavaScript
The Object.values() method returns an array containing the values of an object’s own enumerable string-keyed properties.
Syntax:
Object.values(object);
Example:
const employee = {
name: "Anjali",
age: 25,
department: "IT"
};
console.log(Object.values(employee));
Output:
["Anjali", 25, "IT"]
Unlike Object.keys(), which returns property names, Object.values() returns the values associated with those properties.
Using Object.values() to Calculate a Total
Suppose an object stores the prices of several products.
const prices = {
laptop: 50000,
mouse: 800,
keyboard: 1500
};
const total = Object.values(prices).reduce(function(sum, price) {
return sum + price;
}, 0);
console.log(total);
Output:
52300
Here, Object.values() extracts the prices into an array. The reduce() method then adds them together.
Learn more about processing array values in the JavaScript array methods tutorial.
Object.entries() in JavaScript
The Object.entries() method returns an array of an object’s own enumerable string-keyed properties as key-value pairs.
Each pair is represented as a two-element array: the property name followed by its value.
Syntax:
Object.entries(object);
Example:
const student = {
name: "Rahul",
age: 21,
course: "JavaScript"
};
console.log(Object.entries(student));
Output:
[
["name", "Rahul"],
["age", 21],
["course", "JavaScript"]
]
Using Object.entries() in a Loop
You can use Object.entries() with for...of to access each property name and value together.
const product = {
name: "Laptop",
price: 55000,
brand: "Dell"
};
for (const [key, value] of Object.entries(product)) {
console.log(key + ": " + value);
}
Output:
name: Laptop
price: 55000
brand: Dell
The array destructuring syntax [key, value] assigns the first element of each pair to key and the second to value.
Converting Object Entries Back Into an Object
You can use Object.fromEntries() to create an object from key-value pairs.
const entries = [
["name", "Rahul"],
["age", 21],
["course", "JavaScript"]
];
const student = Object.fromEntries(entries);
console.log(student);
Output:
{
name: "Rahul",
age: 21,
course: "JavaScript"
}
This is useful when transforming object data or rebuilding objects from arrays of entries.
Object.assign() in JavaScript
The Object.assign() method copies enumerable own properties from one or more source objects into a target object. It returns the target object.
Syntax:
Object.assign(target, source1, source2);
Example:
const student = {
name: "Rahul",
age: 21
};
const additionalDetails = {
course: "JavaScript",
city: "Hyderabad"
};
Object.assign(student, additionalDetails);
console.log(student);
Output:
{
name: "Rahul",
age: 21,
course: "JavaScript",
city: "Hyderabad"
}
The properties from additionalDetails are copied into student.
Copying an Object
You can use Object.assign() with an empty object as the target to create a shallow copy.
const original = {
name: "Rahul",
age: 21
};
const copy = Object.assign({}, original);
copy.name = "Anjali";
console.log(original.name);
console.log(copy.name);
Output:
Rahul
Anjali
The top-level properties are copied into a new object, so changing copy.name does not change original.name.
However, Object.assign() creates a shallow copy. If the original object contains nested objects, those nested objects are still shared by reference.
Combining Multiple Objects
You can pass multiple source objects to Object.assign().
const personal = {
name: "Rahul",
age: 21
};
const education = {
course: "JavaScript"
};
const location = {
city: "Hyderabad"
};
const student = Object.assign({}, personal, education, location);
console.log(student);
Output:
{
name: "Rahul",
age: 21,
course: "JavaScript",
city: "Hyderabad"
}
If multiple source objects contain the same property name, the value from the last source object takes precedence.
Object.create() in JavaScript
The Object.create() method creates a new object using a specified prototype object.
A prototype is an object from which another object can inherit properties and methods.
Syntax:
Object.create(prototype);
Example:
const person = {
greet() {
console.log("Hello!");
}
};
const student = Object.create(person);
student.name = "Rahul";
console.log(student.name);
student.greet();
Output:
Rahul
Hello!
The student object has its own name property. It can also access the inherited greet() method through its prototype.
Creating an Object Without a Prototype
You can pass null to create an object that does not inherit from Object.prototype.
const data = Object.create(null);
data.name = "Rahul";
data.age = 21;
console.log(data.name);
console.log(data.age);
Output:
Rahul
21
An object created with Object.create(null) does not inherit standard methods such as toString() from Object.prototype.
This approach is useful for certain dictionary-like data structures, but it is less convenient when you need ordinary object behavior.
Creating an Object With Property Descriptors
Object.create() also accepts an optional second argument that defines properties using property descriptors.
const person = Object.create({}, {
name: {
value: "Rahul",
writable: true,
enumerable: true,
configurable: true
}
});
console.log(person.name);
Output:
Rahul
The descriptor specifies the property’s value and whether it can be changed, enumerated, or reconfigured.
Object.freeze() in JavaScript
The Object.freeze() method prevents an object from being extended and prevents its existing own properties from being reconfigured or having their values changed.
Syntax:
Object.freeze(object);
Example:
const settings = {
theme: "light",
language: "English"
};
Object.freeze(settings);
settings.theme = "dark";
settings.fontSize = 16;
console.log(settings);
Output in non-strict mode:
{
theme: "light",
language: "English"
}
The theme value remains "light", and the new fontSize property is not added.
In strict mode, attempting to change or add a property to a frozen object can throw a TypeError.
Checking Whether an Object Is Frozen
Use Object.isFrozen() to check whether an object is frozen.
const settings = {
theme: "light"
};
Object.freeze(settings);
console.log(Object.isFrozen(settings));
Output:
true
Important Limitation of Object.freeze()
Object.freeze() is shallow. It does not automatically freeze nested objects.
const user = {
name: "Rahul",
address: {
city: "Hyderabad"
}
};
Object.freeze(user);
user.address.city = "Vijayawada";
console.log(user.address.city);
Output:
Vijayawada
The outer object is frozen, but the nested address object is not. Its properties can still be changed unless it is frozen separately.
Object.seal() in JavaScript
The Object.seal() method prevents new properties from being added to an object and prevents existing properties from being deleted or reconfigured. However, existing writable property values can still be updated.
Syntax:
Object.seal(object);
Example:
const user = {
name: "Rahul",
age: 21
};
Object.seal(user);
user.age = 22;
user.city = "Hyderabad";
delete user.name;
console.log(user);
Output in non-strict mode:
{
name: "Rahul",
age: 22
}
The age property changes because its value is writable. The new city property is not added, and the name property is not deleted.
In strict mode, unsuccessful attempts to add or delete properties on a sealed object can throw a TypeError.
Checking Whether an Object Is Sealed
Use Object.isSealed() to check whether an object is sealed.
const user = {
name: "Rahul",
age: 21
};
Object.seal(user);
console.log(Object.isSealed(user));
Output:
true
Difference Between Object.freeze() and Object.seal()
Both methods restrict changes to an object, but they have different effects.
| Feature | Object.freeze() | Object.seal() |
|---|---|---|
| Add new properties | Not allowed | Not allowed |
| Delete existing properties | Not allowed | Not allowed |
| Reconfigure existing properties | Not allowed | Not allowed |
| Update writable property values | Not allowed | Allowed |
| Check method | Object.isFrozen() | Object.isSealed() |
Use Object.freeze() when you want to prevent changes to an object’s own properties. Use Object.seal() when you want to keep the existing set of properties but allow their writable values to change.
Both methods are shallow, so nested objects need separate handling if you want to restrict their modification too.
Summary of JavaScript Object Methods
| Method | Purpose | Return value |
|---|---|---|
Object.keys() | Gets enumerable property names | An array of strings |
Object.values() | Gets enumerable property values | An array |
Object.entries() | Gets property names and values as pairs | An array of pairs |
Object.assign() | Copies properties into a target object | The target object |
Object.create() | Creates an object with a specified prototype | A new object |
Object.freeze() | Prevents changes to an object’s own properties | The same object |
Object.seal() | Prevents adding, deleting, or reconfiguring properties | The same object |
Frequently Asked Questions
What is the difference between Object.keys() and Object.values()?
Object.keys() returns property names, while Object.values() returns the values associated with those properties.
What does Object.entries() do?
It returns an array of key-value pairs for an object’s own enumerable string-keyed properties.
Does Object.assign() create a deep copy?
No. Object.assign() creates a shallow copy when used with an empty target object. Nested objects remain shared references.
What is the difference between Object.create() and Object.assign()?
Object.create() creates an object with a specified prototype. Object.assign() copies enumerable own properties from source objects into a target object.
Can you modify an object after using Object.freeze()?
You cannot change its existing own property values, add properties, or delete properties. Nested objects may still be modified unless they are frozen separately.
Can you update a sealed object?
Yes. Existing writable property values can be updated, but new properties cannot be added and existing properties cannot be deleted or reconfigured.
Are Object.freeze() and Object.seal() deep operations?
No. Both operate on the object itself, not recursively on nested objects.
What to Learn Next
After learning these built-in object methods, continue with JavaScript object destructuring to learn how to extract property values into variables and write cleaner, more readable code.