JS Object Methods

Method

Description

Example

Modifying Object Properties

let person = {
    name: "John",
};
person.age = 30 // {name:"John",age:30}
delete person.name // {age:30}

Get all keys in the object.

let person = {
    name: "John",
    age : 30
};
Object.keys(person) // ["name", "age"]

Get all values in the object.

let person = {
    name: "John",
    age : 30
};
Object.values(person) // ["John", 30]

Get key-value pairs as an array.

let person = {
    name: "John",
    age : 30
};
Object.entries(person) // [["name", "John"], ["age", 31]]

Copy properties from one object to another.

let person = {
    name: "John",
    age : 30
};
const additionalInfo = { profession: "Engineer" };
const updatedPerson = Object.assign(person, additionalInfo);
// {name: "John",age : 30,profession: "Engineer"};

Prevents modifications (add, delete, or update)

let person = {
    name: "John",
};
Object.freeze(person);
person.age = 30 // error will through

Prevents adding or deleting, but allows updating existing properties.

let person = {
    name: "John",
};
Object.seal(person);
person.name = "Doe" // {name : "Doe"}
person.age = 30 // error will through
delete person.name // error will through

Add and delete Object:

Modifying Object Properties

Example
let person = {
name: "John",
};
person.age = 30 // {name:"John",age:30}
delete person.name // {age:30}

Try it yourself


Object.keys():

Get all keys in the object.

Example
let person = {
name: "John",
age : 30
};
Object.keys(person) // ["name", "age"]

Try it yourself


Object.values():

Get all values in the object.

Example
let person = {
name: "John",
age : 30
};
Object.values(person) // ["John", 30]

Try it yourself


Object.entries():

Get key-value pairs as an array.

Example
let person = {
name: "John",
age : 30
};
Object.entries(person) // [["name", "John"], ["age", 31]]

Try it yourself


Object.assign():

Copy properties from one object to another.

Example
let person = {
name: "John",
age : 30
};
const additionalInfo = { profession: "Engineer" };
const updatedPerson = Object.assign(person, additionalInfo);
// {name: "John",age : 30,profession: "Engineer"};

Try it yourself


Object.freeze():

Prevents modifications (add, delete, or update)

Example
let person = {
name: "John",
};
Object.freeze(person);
person.age = 30 // error will through

Try it yourself


Object.seal():

Prevents adding or deleting, but allows updating existing properties.

Example
let person = {
name: "John",
};
Object.seal(person);
person.name = "Doe" // {name : "Doe"}
person.age = 30 // error will through
delete person.name // error will through

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.