JavaScript : Object Methods (English/Hindi)
Tags: webdev, javascript, beginners, programming
Published: 2023-09-15 03:01:59

## English

**.keys():**
This method returns an array of all the keys (property names) of an object.

```javascript
// Example:
const person = {
  name: "John",
  age: 30,
  city: "New York"
};

const keys = Object.keys(person);
console.log(keys); // Output: ["name", "age", "city"]
```

**.values():**
This method returns an array of all the values of an object.

```javascript
// Example:
const person = {
  name: "John",
  age: 30,
  city: "New York"
};

const values = Object.values(person);
console.log(values); // Output: ["John", 30, "New York"]
```

**.entries():**
This method returns an array of key-value pairs (as arrays) of an object.

```javascript
// Example:
const person = {
  name: "John",
  age: 30,
  city: "New York"
};

const entries = Object.entries(person);
console.log(entries);
// Output: [["name", "John"], ["age", 30], ["city", "New York"]]
```

**.assign():**
This method is used to copy the values of all enumerable properties from one or more source objects to a target object.

```javascript
// Example:
const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };

const result = Object.assign(target, source);
console.log(target); // Output: { a: 1, b: 3, c: 4 }
```

**.hasOwnProperty():**
This method checks if an object has a specific property and returns a boolean value.

```javascript
// Example:
const person = {
  name: "John",
  age: 30
};

const hasAge = person.hasOwnProperty("age");
console.log(hasAge); // Output: true
const hasCity = person.hasOwnProperty("city");
console.log(hasCity); // Output: false
```


## Hindi

**.keys()**
Yeh method ek object ke saare keys (properties) ka ek array return karta hai.

```javascript
const vyakti = {
  naam: "John",
  umar: 30,
  shehar: "New York"
};

const chabiyonKaArray = Object.keys(vyakti);
console.log(chabiyonKaArray); // Output: ["naam", "umar", "shehar"]
```

**.values():**
Yeh method ek object ke saare values ka ek array return karta hai.

```javascript
const vyakti = {
  naam: "John",
  umar: 30,
  shehar: "New York"
};

const moolyonKaArray = Object.values(vyakti);
console.log(moolyonKaArray); // Output: ["John", 30, "New York"]
```

**.entries():**
Yeh method ek object ke key-value pairs (arrays ke roop mein) ka array return karta hai.

```javascript
const vyakti = {
  naam: "John",
  umar: 30,
  shehar: "New York"
};

const entryArray = Object.entries(vyakti);
console.log(entryArray);
// Output: [["naam", "John"], ["umar", 30], ["shehar", "New York"]]
```

**.assign();**
Yea method ek ya adhik objects ke properties ko ek naye object mein milakar usse combine karta hai.

```javascript
const pehlaObject = {
  naam: "Shyam",
  umar: 25
};

const doosraObject = {
  shehar: "Mumbai",
  kaam: "Engineer"
};

// `Object.assign()` se do objects ko combine karenge aur ek naya object banayenge.
const combinedObject = Object.assign({}, pehlaObject, doosraObject);

console.log(combinedObject);
// Output: { naam: "Shyam", umar: 25, shehar: "Mumbai", kaam: "Engineer" }
```

**.hasOwnProperty():**
Yea method ek object ke andar kisi property ka maujood hone ko check kar sakte hain. Is method ka upayog kisi specific property ke existance ko verify karne ke liye hota hai. Yadi woh property object ke khud ke hai, tabhi yeh method true return karega, anyatha false return karega.

```javascript
const vyakti = {
  naam: "Shyam",
  umar: 25,
  shehar: "Mumbai"
};

// Check karein ki 'naam' property vyakti object ke andar maujood hai ya nahi
const kyaNaamHai = vyakti.hasOwnProperty('naam');
console.log(kyaNaamHai); // Output: true

// Check karein ki 'address' property vyakti object ke andar maujood hai ya nahi
const kyaAddressHai = vyakti.hasOwnProperty('address');
console.log(kyaAddressHai); // Output: false
```