getters and setters javascript

JavaScript
let obj = {
  log: ['a', 'b', 'c'],
  get latest() {
    if (this.log.length === 0) {
      return undefined;
    }
    return this.log[this.log.length - 1];
  }
};

obj.log.push('d');
console.log(obj.latest); //output: 'd'
Source

Also in JavaScript: