# JavaScript Object Getter and Setter Methods

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

---
### Getter
Getter methods are used to return an object's properties. 

See the examples below:

```js
const person = {
  name: 'Bello',
  age: 27,
  getPerson() {
    if (typeof this.name === 'string' && typeof this.age === 'number') {
      return `${this.name} is ${this.age} years old!`;
    }

    return "Name or age is invalid";
  } 
}

console.log( person.getPerson() );
```

We can add a little more `get` keyboard to the code snippet above.

```js
const person = {
  name: 'Bello',
  age: 27,
  get getPerson() {
    if (typeof this.name === 'string' && typeof this.age === 'number') {
      return `${this.name} is ${this.age} years old!`;
    }

    return "Name or age is invalid";
  } 
}

console.log(person.getPerson); //  No parenthesis used
```
The difference is when the `get` keyword was used, no parenthesis was used in the calling function.

> `getPerson`, not ` getPerson()`."

#### The *name* property
A function in on object looks like a property when the `get` keyword is used.

See the example:

```js
const sayHi = {
  get name() {
    return "Hello World!";
  }
};

sayHi.name; // "Hello World!"
```

The syntax above looks like `object.property` syntax.

We can get the name of a function with the `name` property.

See the example below:

```js
function greet() {
  return "Hello World!";
}

console.log(greet.name); // greet
```

A function name can also be accessible even if used as a default parameter in another function.

```js
function func(greet = function() {}) {
  console.log(greet.name); // greet
}

func();
```

#### The *length* property
The length or the number of parameters of a function can also be accessible by the `length` property.

See the example below:

```js
const func = (name, age) => {
  return "${name} is ${age} today!";
}

console.log(func.length); // 2
```

---

### Setter
Setter methods are used to `set` or reassign values of existing properties within an object.

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

person.name = 'John';
console.log(person.name) // John;
```

We can do more than the example above by checking if certain properties exist in an object. 
In the example below, an existing property gets reassigned

```js
const person = {
  name: 'Bello',

  set setPerson(name) {
    if (this.name) {
      this.name = name;
    } else { 
      console.log("Give a valid name");
    }
  } 

};

person.setPerson = 'John';
console.log(person.name, typeof person.name) // John string
``` 

**Happy coding!!!**

---
[![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1613214479941/H06CZLBzo.png)](https://www.buymeacoffee.com/techstackmedia)

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