JavaScript Enhanced Object Literals Interview Questions and Answers
Review property shorthand, method shorthand, and computed property names in object literals.
Frequently Asked Questions
Important questions are highlighted in the sections below; select one to jump to its answer.
- What are property and method shorthand in object literals?
- What is method shorthand in an object literal?
- How do computed property names work in object literals?
- When can an object literal use property shorthand?
- How does a computed property name work?
- How are computed object property keys converted?
- What determines `this` in a concise object method?
Theoretical JavaScript Interview Questions
Property shorthand uses a variable name as both the property name and value when they match. Method shorthand defines a function directly on the object. These forms make object literals more concise without changing the resulting property behavior.
const name = 'Ada';
const user = { name, greet() { return `Hello, ${this.name}`; } };
console.log(user.greet()); // Hello, AdaA computed property name uses square brackets to evaluate an expression for the property key when the object is created. The result is converted to a property key, which is useful when a key comes from a variable.
const key = 'theme';
const settings = { [key]: 'dark' };
console.log(settings.theme); // darkWhen a property name matches an in-scope variable, `{ name }` is shorthand for `{ name: name }`. The shorthand still creates an ordinary own property on the new object.
Method shorthand defines a function-valued property as `greet() { ... }` instead of `greet: function () { ... }`. It also permits concise methods to use `super` when the object has an appropriate prototype.
Square brackets evaluate an expression to produce a property key when the object literal is created. For example, `[key]` uses the current value of `key` as the property name.
Computed property keys are converted to property keys: strings remain strings, symbols remain symbols, and other values are converted to strings. Thus numeric keys such as `1` address the string key `"1"`.
A concise method is not an arrow function, so `this` is determined by how it is called. Calling it as `object.method()` binds `this` to `object`; extracting and calling it separately does not preserve that receiver.
A getter runs when its property is read, and a setter runs when it is assigned. They define accessor properties rather than storing a value automatically, so implementations often use a separate backing property.
The `__proto__` literal form can set the new object's prototype to an object or `null`. It is a special object-literal syntax, unlike assigning to a property named `__proto__` through computed syntax.
A getter creates an own accessor property with a getter function and no setter unless one is also defined. Object-literal properties are otherwise configurable and enumerable by default.
A non-computed `__proto__: value` entry changes the prototype of the object being created when the value is an object or `null`; it does not create an own property. A computed key such as `["__proto__"]` creates an ordinary own data property.
`super` in a concise method looks up a property on the method's home object's current prototype, while the call receiver remains the method's `this`. It is not simply an alias for a fixed parent object.
Object-literal property definitions are evaluated in source order, including their computed key expressions and value expressions. Side effects in those expressions therefore occur during object creation in that order.
JavaScript Practical and Coding Interview Questions
Problem: Create a configuration object that uses shorthand for `debug`, a computed property named by `optionName`, and a concise `describe` method. Call the method and print the value.
const debug = true;
const optionName = "retryLimit";
const retryLimit = 3;
const config = {
debug,
[optionName]: retryLimit,
describe() {
return `Retries: ${this[optionName]}`;
}
};
console.log(config.describe());Problem: Create an object literal with a `score` getter and setter backed by a closure variable. The setter should accept only finite non-negative numbers and leave the previous score unchanged for invalid input. Demonstrate both cases.
let storedScore = 0;
const player = {
get score() {
return storedScore;
},
set score(value) {
if (Number.isFinite(value) && value >= 0) {
storedScore = value;
}
}
};
player.score = 12;
console.log(player.score);
player.score = -1;
console.log(player.score);