Last Updated : 12 Jul, 2025
The Object.defineProperties() method in JavaScript is a standard built-in Object that defines a new or modifies existing properties directly on an object and it returns the object.
Syntax:Object.defineProperties(obj, props)Parameters:
Example 1: In this example, we will define new properties to an object using the Object.defineProperties() method in JavaScript.
javascript
const geek = {};
Object.defineProperties(geek, {
prop1: {
value: "geeksforgeeks",
writable: true
},
prop2: {}
});
console.log(geek.prop1);
console.log(geek.prop2);
Output:
"geeksforgeeks"
undefined
Example 2: In this example, we will define new properties and modify some properties of an object using the Object.defineProperties() method in JavaScript.
javascript
let geek = {};
Object.defineProperties(geek, {
'prop1': {
value: "Geeks",
writable: true
},
'prop2': {
value: 'Hello',
writable: false
}
});
console.log(geek.prop2);
console.log(geek.prop1);
const geek1 = {};
Object.defineProperties(geek1, {
prop1: {
value: "Hi",
value: "Hello",
value: "Namaste",
},
prop2: {
value: " And ",
},
prop3: {
value: "learn",
value: "Stay Safe",
}
});
console.log(geek1.prop1, geek1.prop2, geek1.prop3);
Output:
"Hello"
"Geeks"
"Namaste" " And " "Stay Safe"
We have a complete list of Javascript Object methods, to check those please go through this JavaScript Object Complete Reference article.
Supported Browsers:The browsers supported by Object.defineProperties() method are listed below:
CanThe Object.defineProperties() method defines new or modifies existing properties directly on an object, returning the object.
Object.defineProperties()
be used to freeze properties?
How doesYou can create non-writable and non-configurable properties using
Object.defineProperties()
, effectively making them immutable.
Object.defineProperties()
handle deeply nested properties?
How does
Object.defineProperties()
only defines or modifies properties at the top level of the object. It does not traverse nested objects.
Object.defineProperties()
interact with strict mode?
What is the most common use of theIn strict mode, attempting to define or modify properties in a way that violates property attributes will throw an error.
Object.defineProperties()
method?
- Batch Property Definition: Defining multiple properties with specific attributes at once.
- Controlled Property Behavior: Customizing property behavior with attributes like
writable
,enumerable
, andconfigurable
.- Implementing Getters and Setters: Creating dynamic properties with getter and setter functions.
- Updating Object Structures: Modifying and updating properties of existing objects efficiently.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4