jskatas.org Continuously Learn JavaScript. Your Way.

Object literal: getter

A getter binds an object property to a function that will be called when that property is looked up.

An object literal can also contain getters

just prefix the property with get (and make it a function)
const obj = { x() { return 'ax'; } }; assert.equal(obj.x, 'ax');
must have NO parameters
const obj = { x(param) { return 'ax'; } }; assert.equal(obj.x, 'ax');
can be a computed property (an expression enclosed in [])
const keyName = 'x'; const obj = { get keyName() { return 'ax'; } }; assert.equal(obj.x, 'ax');
can be removed using delete
const obj = { get x() { return 'ax'; } }; delete obj.y; assert.equal(obj.x, void 0);


Description of all the details of a getter.
"An accessor property associates a key value with one or two accessor functions ..."
Announcement of this kata on twitter.

Required Knowledge

Related Katas

Global Object API

Object API

Object literal

Difficulty Level


First Published

27 July 2015


4 tests to solve