What is this method of accessing a property dynamically within the state object called?

const activeSession = action.payload.toLowerCase();
return {
[activeSession]: state[activeSession] + 1

Here, at runtime activeSession is one of DAYS, MINUTES, HOURS etc. We’re using square brackets to substitute the correct value at runtime on the left hand side of the expression [activeSession]: state[activeSession] + 1. What is this method called?

1 Like

Hi @shaunak, Thank you for contacting the Educative team with your query!

The method of accessing a property dynamically within an object using a variable is commonly referred to as dynamic property access or dynamic property name in JavaScript. In the example, state[activeSession] is an example of dynamic property access, where the property name is determined at runtime based on the value of the activeSession variable.

Hope the answer to your query. Please feel free to reply here if there is still some confusion.

Hope you have an amazing learning experience on Educative! :blush:

1 Like

Thanks for answering my question.
For anyone else new to this pattern, it’s an ES6 feature called key interpolation (explained here: Key Interpolation Javascript - Stack Overflow).

1 Like