Object.defineProperties() method - JavaScript Tutorial

Object.defineProperties() method

BLUF: This tutorial on Object.defineProperties() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: Object.defineProperties() method

Understanding Object.defineProperties() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The Object.defineProperties function is utilized to create new properties or alter existing ones directly on an object, returning the modified object.

Syntax:

Example

Object.defineProperties(obj, props)

Parameter

Obj : The entity upon which properties can be established or altered.

Props: An entity whose own enumerable attributes serve as descriptors for the properties that are intended to be defined or altered.

Return:

This technique returns an object that was provided as an argument to the function.

Browser Support:

Chrome Yes
Edge Yes
Firefox 1.5
Opera Yes

Example 1

Example

const object1 = {};
Object.defineProperties(object1, {
  property1:{
value: 44, }
 });
console.log(object1.property1);

Output:

Example 2

Example

const object1 = {};
Object.defineProperties(object1, {
  property1: {
    value: 142,
    value: 422,
    value: 423,
    },
 property2: {}
});
console.log(object1.property1);

Output:

Example 3

Example

const obj = {};
Object.defineProperties(obj, {
  property1: {
    value: 142,
    value: 422,
    value: 423,
    },
  property2: {
  value: 22,
  value: 12,}
});
console.log(obj.property1,obj.property2);

Output:

Output

423 
12

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience