JavaScript Built-in Object Methods
Tags: javascript, tutorial, webdev, codenewbie
Published: 2021-02-14 20:31:58

[![host.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613285550606/kVnFkQSP1.png)](https://partners.hostgator.com/MaZ1N)

---
We have already seen how to create objects and object methods, but there are built-in methods that JavaScript makes available for use. 

Below are examples of a few methods:

```js
const person = {
    name: 'Bob', 
    age: 54, 
    country: 'London'
};

const toArray = Object.values(person);
console.log(toArray); // [ 'Bob', 54, 'London' ]
```

```js
const person = {
    name: 'Bello', 
    age: 27, 
    country: 'Nigeria'
};

const toStr = JSON.stringify(person);
console.log(toStr);
// {"name":"Bello","age":54,"country":"Nigeria"}

toStr[0]; // '{'
toStr[1]; // "
toStr[2]; // n
```

`JSON.stringify` will not stringify functions:

```js
const person = {
    name: 'Bello', 
    age: function() {
      return 27;
    }, 
    country: 'Nigeria'
};

const toStr = JSON.stringify(person);
console.log(toStr);
// {"name":"Bello","country":"Nigeria"}
```

We have to convert the function to string (`toString`) first.

```js
const person = {
    name: 'Bello', 
    age: function() {
      return 27;
    }, 
    country: 'Nigeria'
};

const str = person.age.toString();
console.log(str);
/* 
function() {
    return 27;
}
 */

const toStr = JSON.stringify(person);
console.log(toStr);
/* 
function() {
    return 27;
}
{"name":"Bello","country":"Nigeria"}
 */
```

> Both arrays and objects are non-primitive data types as objects. Therefore a few object methods can work on arrays also. Like `JSON.stringify()`

```js
const arr = [ 'Bello', 27, 'Nigeria' ];

const toStr = JSON.stringify(arr);
console.log(toStr); // ["Bello",27,"Nigeria"]

toStr[0]; // "["
toStr[2]; // "B"
```

More on arrays later.

```js
const person = {
    name: 'Bob', 
    age: 54, 
    country: 'London'
};

const hasProp = person.hasOwnProperty('name'); 
hasProp; //true
```

---

[![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613214987327/HP8_p_PnK.png)](https://domain.mno8.net/c/2653712/848392/9560)

---
`Object.assign()` copies all objects properties into a new object

```js
const obj1 = {
  a: 1,
  b: 2,
  c: 3
};

const obj2 = {
  d: 4,
  e: 5
}

const newObj = Object.assign(obj1, obj2);
newObj; // { a: 1, b: 2, c: 3, d: 4, e: 5 }
```

The general syntax is:

```
newObj = Object.assign(obj1[, obj2, ...,objN]);
```
 
A similar new object property name overrides the old object property name.

```
Object.assign(oldObj, newObj)
```

```js
const obj1 = {
  a: 1,
  b: 2,
  c: 3
};

const obj2 = {
  c: 100,
  e: 5
}

const newObj = Object.assign(obj1, obj2);
newObj; // { a: 1, b: 2, c: 100, e: 5 }
```

> `c: 100` overrides `c: 3`

```js
const person = {
    name: 'Bob', 
    age: 54, 
    country: 'London'
};

const objKeys = Object.keys(person);
console.log(objKeys); // [ 'name', 'age', 'country' ]
```

The `entries` method used on an object converts the object, property keys, and property values to an array.

Syntax:

```
[ [key1, value1], [key2, value2], ...[keyN], [valueN] ]
```

```js
const person = {
    name: 'Bob', 
    age: 54, 
    country: 'London'
};

const objEntries = Object.entries(person);
console.log(objEntries); 
// [ [ 'name', 'Bob' ], [ 'age', 54 ], [ 'country', 'London' ] ]

objEntries[1]; // [ 'age', 54 ]
objEntries[1][0]; // "age"
```

All built-in static object methods can be found on [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#static_methods).

**Happy coding!!!**

---

[![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613216267892/cgCrviTiF.png)](https://www.buymeacoffee.com/techstackmedia)

---

<a href="https://skillshare.eqcm.net/c/2680282/300216/4650" target="_top" id="300216"><img src="//a.impactradius-go.com/display-ad/4650-300216" border="0" alt="Learn on Skillshare" width="300" height="250"/></a><img height="0" width="0" src="https://skillshare.eqcm.net/i/2680282/300216/4650" style="position:absolute;visibility:hidden;" border="0" />