Mastering JavaScript Object Methods
Table of contents
- JavaScript Objects
- 1. Object.keys() and Object.values(): Extracting Keys and Values
- 2. Object.entries(): Extracting Key-Value Pairs
- 3. Object.assign(): Combining Objects
- 4. Object.hasOwnProperty(): Checking Property Existence
- 5. Object.freeze() and Object.seal(): Imposing Restrictions
- 6. Object.create(): Creating Objects
- Contact Me
- Conclusion
By Rubel Mehmed
JavaScript Objects
An object in JavaScript is a collection of key-value pairs. Each key is a string (or Symbol), and each value can be of any data type. Objects are often used to represent real-world entities and provide a structured way to store and access data.
JavaScript provides a variety of built-in methods that make working with objects a breeze. Let's dive into some of the most commonly used object methods:
1. Object.keys()
and Object.values()
: Extracting Keys and Values
Object.keys()
returns an array of a given object's property names (keys).Object.values()
returns an array of a given object's property values.
const person = {
firstName: 'John',
lastName: 'Doe',
age: 30,
};
const keys = Object.keys(person); // ['firstName', 'lastName', 'age']
const values = Object.values(person); // ['John', 'Doe', 30]
2. Object.entries()
: Extracting Key-Value Pairs
Object.entries()
returns an array of a given object's own enumerable property[key, value]
pairs.
const person = {
firstName: 'John',
lastName: 'Doe',
age: 30,
};
const entries = Object.entries(person);
// [['firstName', 'John'], ['lastName', 'Doe'], ['age', 30]]
3. Object.assign()
: Combining Objects
Object.assign()
copies the values of all enumerable properties from one or more source objects to a target object.
const target = {};
const source1 = { a: 1 };
const source2 = { b: 2 };
Object.assign(target, source1, source2);
// target: { a: 1, b: 2 }
4. Object.hasOwnProperty()
: Checking Property Existence
Object.hasOwnProperty()
checks if a given object has a property with a specified key.
const person = {
firstName: 'John',
lastName: 'Doe',
};
console.log(person.hasOwnProperty('firstName')); // true
console.log(person.hasOwnProperty('age')); // false
5. Object.freeze()
and Object.seal()
: Imposing Restrictions
Object.freeze()
prevents any changes to an object, including adding, modifying, or deleting properties.Object.seal()
prevents adding new properties and marks all existing properties as non-configurable.
const person = {
firstName: 'John',
};
Object.freeze(person);
person.lastName = 'Doe'; // This assignment is ignored in strict mode
const sealedPerson = Object.seal(person);
delete sealedPerson.firstName; // Cannot delete properties
6. Object.create()
: Creating Objects
Object.create()
creates a new object with the specified prototype object.
const personPrototype = {
greet() {
console.log(`Hello, my name is ${this.name}`);
},
};
const john = Object.create(personPrototype);
john.name = 'John';
john.greet(); // 'Hello, my name is John'
Contact Me
GitHub: GitHub - Rubel Mehmed
LinkedIn: LinkedIn - Rubel Mehmed
Portfolio: Rubel Mehmed's Portfolio
Conclusion
JavaScript object methods provide the tools you need to work with objects efficiently, allowing you to extract, combine, and manipulate data effortlessly. Whether you're building web applications, working with APIs, or handling complex data structures, mastering these methods is essential.
By incorporating these object methods into your JavaScript projects, you can write more organized, maintainable code and unlock the full potential of your applications.
Thank you for reading this comprehensive guide to JavaScript object methods. I hope you find it informative and helpful in your programming journey. If you have any questions or feedback, please feel free to reach out.
Happy coding!
Rubel Mehmed
Subscribe to my newsletter
Read articles from Rubel Mehmed directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Rubel Mehmed
Rubel Mehmed
I am a Frontend Engineer from Bangladesh