✨ Important JavaScript Object Methods

Mukesh Prajapati
2 min readNov 25, 2022

--

Javascript Object methods which every developer should know, to boost up their code and skills.

1. Object.keys()

A simple way to iterate over an object and return all of the object’s keys

const employee = {name: "Daniel", age: 40, occupation: "Engineer", level: 4};

console.log(Object.keys(employee));

// Result:
// [object array] (4)
["name", "age", "occupation", "level"]

2. Object.values()

Iterates over the object and returns the object’s values!

const employee = {name: "Daniel", age: 40, occupation: "Engineer", level: 4};

console.log(Object.values(employee));

// Result:
// [object array] (4)
["Daniel", 40, "Engineer", 4]

3. Object.entries()

Takes an object and returns its own enumerable string-keyed property [key, value] pairs of the object.

const drinks = {
apple: "out of stock",
orange: 3.5
};

console.log(Object.entries(drinks));

// Result:
// [object array] (2)
0: (2) ["apple", "out of stock"]
1: (2) ["orange", 3.5]

// for better understanding

for (const [name, cost] of Object.entries(drinks)) {
console.log(`${name}: ${cost}`);
}

// Result:

"apple": "out of stock"
"orange": 3.5

4. Object.create()

Create a new object, using an existing object as the prototype of the newly created object

const user = {
name: "Daniel",
display: (){
console.log("Name": this.name);
}
}

let newUser = Object.create(user);
newUser.name = "Jone";

// log both object

newUser.display();
// Result: "Name": "Jone"

user.display();
// Result: "Name": "Daniel"

5. Object.assign()

Copies all enumerable and own properties from the source object to the target object. It returns the target object. It is also called shallow copy.

const target = {a: 1, b: 2};
const source = {b: 5, c: 6};

const returnedTarget = Object.assign(target, source);

console.log(target, "target");
console.log(source, "source");
console.log(returnedTarget, "returnedTarget");

//Result:

// [object Object]
{
a: 1,
b: 5,
c: 6
}
// this object returned by target and returnedTarget const

{b:5, c: 6} // source

6. Object.seal()

Seals an object which prevents new properties from being added to it and marks all existing properties as nonconfigurable.

const car = {
price: 15000
};

Object.seal(car);
car.price = 18000; // value changed successfully
console.log(car.price);

// Result: 18000

delete car.price;
console.log(car.price); // Can't delete when sealed

// Result: 18000

7. Object.freeze()

Freezes an object. A frozen object can no longer be changed, this means:

  1. New properties from being added to the object.
  2. Existing properties to be removed from the object.
  3. Changing the enumerability, configurability, or writability of existing properties.
  4. Changing values of the existing object properties and prototype.
const client = {
budget: 3000
};

Object.freeze(client);

client.budget = 2500; // Shows an error in strict mode
console.log(client.budget);

// Result: 3000 => unchanged value as output

Thanks for reading 🙏

--

--