Objects

Computed Property Names

Computed Property Names

An object literal can use a value as a property name. Square brackets inside the literal mean "evaluate this expression, then use the result as the key."

This is useful for analytics, form state, translation tables, and any data shape where the key is chosen at runtime.

Your task: Write recordMetric(name, value) so it returns an object with one property. The property key should come from name, and the property value should come from value. The function should not always return the same key.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Computed Property Names

An object literal can use a value as a property name. Square brackets inside the literal mean "evaluate this expression, then use the result as the key."

This is useful for analytics, form state, translation tables, and any data shape where the key is chosen at runtime.

Your task: Write recordMetric(name, value) so it returns an object with one property. The property key should come from name, and the property value should come from value. The function should not always return the same key.

Free with a JS Exercises account

Sign in to start coding, run your solution, and keep your progress saved for later. No payment needed.

By continuing, you agree to our Terms and Privacy Policy. We'll send occasional product updates and news. You can unsubscribe anytime.

Console

Run code to see output.