1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953---
title: "Computed Property Names: Dynamic Object Keys in JavaScript"
sidebarTitle: "Computed Property Names"
description: "Learn JavaScript computed property names. Create dynamic object keys with variables, expressions, Symbols, and computed methods for cleaner ES6+ code."
---
Have you ever needed to create an object where the property name comes from a variable? Before ES6, this required creating the object first, then adding the property in a separate step. Computed property names changed everything.
```javascript
// Before ES6 - two steps required
const key = 'status';
const obj = {};
obj[key] = 'active';
// ES6 computed property names - single expression
const key2 = 'status';
const obj2 = { [key2]: 'active' };
console.log(obj2); // { status: 'active' }
```
With **computed property names**, you can use any expression inside square brackets `[]` within an object literal, and JavaScript evaluates that expression to determine the property name. This seemingly small syntax addition enables powerful patterns for dynamic object creation.
<Info>
**What you'll learn in this guide:**
- What computed property names are and their ES6 syntax
- How JavaScript evaluates computed keys (order of evaluation)
- Dynamic keys with variables and expressions
- Using Symbol keys for unique, non-colliding properties
- Computed method names, getters, and setters
- Common patterns: form handling, state updates, internationalization
- Edge cases: duplicate keys, type coercion, and the `__proto__` gotcha
</Info>
<Warning>
**Prerequisite:** This guide assumes familiarity with [object basics](/concepts/primitive-types) and [bracket notation](/concepts/modern-js-syntax) for property access. Some examples use [Symbols](/beyond/concepts/javascript-type-nuances), which are covered in detail in the Symbol Keys section.
</Warning>
---
## What are Computed Property Names?
**Computed property names** are an ES6 feature that allows you to use an expression inside square brackets `[]` within an object literal to dynamically determine a property's name at runtime. The expression is evaluated, converted to a string (or kept as a Symbol), and used as the property key. This enables creating objects with dynamic keys in a single expression, eliminating the need for the two-step create-then-assign pattern required before ES6.
```javascript
const field = 'email';
const value = 'alice@example.com';
// The expression [field] is evaluated to get the key name
const formData = {
[field]: value,
[`${field}_verified`]: true
};
console.log(formData);
// { email: 'alice@example.com', email_verified: true }
```
Think of computed property names as **dynamic labels** for your object's filing cabinet. Instead of pre-printing labels (static keys), you're using a label maker (the expression) to print the label right when you create the file.
---
## The Dynamic Label Analogy
Imagine you're organizing a filing cabinet. With traditional object literals, you must know all the label names in advance. With computed properties, you can generate labels on the fly.
```
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β COMPUTED PROPERTY NAMES: DYNAMIC LABELS β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β STATIC KEYS (Traditional) COMPUTED KEYS (ES6) β
β βββββββββββββββββββββββββ ββββββββββββββββββββββ β
β β
β Pre-printed labels: Label maker: β
β ββββββββββββββββββββ ββββββββββββββββββββ β
β β name: "Alice" β β [key]: "Alice" β β
β β age: 30 β β [prefix+id]: 30 β β
β ββββββββββββββββββββ ββββββββββββββββββββ β
β β β
β You must know "name" key can be any β
β and "age" at write time expression evaluated β
β at runtime β
β β
β const obj = { const key = 'name'; β
β name: "Alice", const obj = { β
β age: 30 βββββββββββββββΊ [key]: "Alice", β
β }; [`user_${key}`]: "Alice" β
β }; β
β β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
```
---
## Basic Syntax
The syntax is straightforward: wrap any expression in square brackets `[]` where you would normally write a property name.
### Variable as Key
The most common use case is using a variable's value as the property name:
```javascript
const propName = 'score';
const player = {
name: 'Alice',
[propName]: 100
};
console.log(player); // { name: 'Alice', score: 100 }
console.log(player.score); // 100
```
### Template Literal as Key
Template literals let you build dynamic key names with string interpolation:
```javascript
const prefix = 'user';
const id = 42;
const data = {
[`${prefix}_${id}`]: 'Alice',
[`${prefix}_${id}_role`]: 'admin'
};
console.log(data);
// { user_42: 'Alice', user_42_role: 'admin' }
```
### Expression as Key
Any valid JavaScript expression works inside the brackets:
```javascript
const i = 0;
const obj = {
['prop' + (i + 1)]: 'first',
['prop' + (i + 2)]: 'second',
[1 + 1]: 'number key'
};
console.log(obj);
// { '2': 'number key', prop1: 'first', prop2: 'second' }
```
### Function Call as Key
You can even call functions to generate key names:
```javascript
function getKey(type) {
return `data_${type}_${Date.now()}`;
}
const cache = {
[getKey('user')]: { name: 'Alice' }
};
console.log(Object.keys(cache)[0]);
// Something like: 'data_user_1699123456789'
```
---
## How the Engine Evaluates Computed Keys
Understanding the evaluation order is crucial for avoiding subtle bugs.
### Order of Evaluation: Key Before Value
When JavaScript encounters a computed property, it evaluates the **key expression first**, then the **value expression**. Properties are processed left-to-right in source order.
```javascript
let counter = 0;
const obj = {
[++counter]: counter, // key: 1, value: 1
[++counter]: counter, // key: 2, value: 2
[++counter]: counter // key: 3, value: 3
};
console.log(obj);
// { '1': 1, '2': 2, '3': 3 }
```
Each property's key expression (`++counter`) is evaluated before its value expression (`counter`), so the key and value end up with the same number.
### Type Coercion: ToPropertyKey()
Property keys can only be **strings** or **Symbols**. When you use any other type, JavaScript converts it using an internal operation called `ToPropertyKey()`:
| Input Type | Conversion |
|------------|------------|
| String | Used as-is |
| Symbol | Used as-is |
| Number | Converted to string: `42` β `"42"` |
| Boolean | `true` β `"true"`, `false` β `"false"` |
| null | `"null"` |
| undefined | `"undefined"` |
| Object | Calls `toString()` β usually `"[object Object]"` |
| Array | Calls `toString()` β `[1,2,3]` becomes `"1,2,3"` |
```javascript
const obj = {
[42]: 'number',
[true]: 'boolean',
[null]: 'null',
[[1, 2, 3]]: 'array'
};
console.log(obj);
// { '42': 'number', 'true': 'boolean', 'null': 'null', '1,2,3': 'array' }
// Number keys and string keys can collide!
console.log(obj[42]); // 'number'
console.log(obj['42']); // 'number' (same property!)
```
<Warning>
**Common gotcha:** Number and string keys that convert to the same string refer to the same property. `obj[1]` and `obj['1']` access the same property.
</Warning>
---
## Before ES6: The Two-Step Pattern
Before computed property names, creating objects with dynamic keys required multiple steps:
```javascript
// ES5: Create object, then add dynamic property
function createUser(role, name) {
var obj = {};
obj[role] = name;
return obj;
}
var admin = createUser('admin', 'Alice');
console.log(admin); // { admin: 'Alice' }
```
This was especially awkward in situations requiring single expressions:
```javascript
// ES5: IIFE pattern for single-expression dynamic keys
var role = 'admin';
var users = (function() {
var obj = {};
obj[role] = 'Alice';
return obj;
})();
// ES6: Clean single expression
const role2 = 'admin';
const users2 = { [role2]: 'Alice' };
```
The ES6 syntax shines in:
- **Default function parameters** that need dynamic objects
- **Arrow functions** with implicit returns
- **Const declarations** requiring immediate initialization
- **Array methods** like `map()` and `reduce()`
```javascript
// ES6 enables elegant patterns
const fields = ['name', 'email', 'age'];
const defaults = fields.reduce(
(acc, field) => ({ ...acc, [field]: '' }),
{}
);
console.log(defaults);
// { name: '', email: '', age: '' }
```
---
## Symbol Keys: The Primary Use Case
Symbols are unique, immutable identifiers that can **only** be used as object keys via computed property syntax. This is one of the most important use cases for computed properties.
### Why Symbols Need Computed Syntax
You cannot use a Symbol with the shorthand or colon syntax:
```javascript
const mySymbol = Symbol('id');
// This creates a string key "mySymbol", NOT a Symbol key!
const wrong = { mySymbol: 'value' };
console.log(Object.keys(wrong)); // ['mySymbol']
// This uses the Symbol as the key
const correct = { [mySymbol]: 'value' };
console.log(Object.keys(correct)); // [] (Symbols don't appear in keys!)
console.log(Object.getOwnPropertySymbols(correct)); // [Symbol(id)]
```
### Symbol Keys Are Hidden
Symbol-keyed properties don't appear in most iteration methods:
```javascript
const secret = Symbol('secret');
const user = {
name: 'Alice',
[secret]: 'classified information'
};
// Symbol keys are hidden from these:
console.log(Object.keys(user)); // ['name']
console.log(JSON.stringify(user)); // '{"name":"Alice"}'
for (const key in user) {
console.log(key); // Only logs 'name'
}
// But you can still access them:
console.log(user[secret]); // 'classified information'
console.log(Object.getOwnPropertySymbols(user)); // [Symbol(secret)]
```
### Well-Known Symbols: Customizing Object Behavior
JavaScript has built-in "well-known" Symbols that let you customize how objects behave. These must be used with computed property syntax.
#### Symbol.iterator: Make Objects Iterable
```javascript
const range = {
start: 1,
end: 5,
[Symbol.iterator]() {
let current = this.start;
const end = this.end;
return {
next() {
if (current <= end) {
return { value: current++, done: false };
}
return { done: true };
}
};
}
};
console.log([...range]); // [1, 2, 3, 4, 5]
for (const num of range) {
console.log(num); // 1, 2, 3, 4, 5
}
```
#### Symbol.toStringTag: Custom Type String
```javascript
const myCollection = {
items: [],
[Symbol.toStringTag]: 'MyCollection'
};
console.log(Object.prototype.toString.call(myCollection));
// '[object MyCollection]'
// Compare to a plain object:
console.log(Object.prototype.toString.call({}));
// '[object Object]'
```
#### Symbol.toPrimitive: Custom Type Coercion
```javascript
const temperature = {
celsius: 20,
[Symbol.toPrimitive](hint) {
switch (hint) {
case 'number':
return this.celsius;
case 'string':
return `${this.celsius}Β°C`;
default:
return this.celsius;
}
}
};
console.log(+temperature); // 20 (number hint)
console.log(`${temperature}`); // '20Β°C' (string hint)
console.log(temperature + 10); // 30 (default hint)
```
### Privacy Patterns with Symbols
While not truly private, Symbol keys provide a level of encapsulation:
```javascript
// Module-scoped Symbol - not exported
const _balance = Symbol('balance');
class BankAccount {
constructor(initial) {
this[_balance] = initial;
}
deposit(amount) {
this[_balance] += amount;
}
getBalance() {
return this[_balance];
}
}
const account = new BankAccount(100);
console.log(Object.keys(account)); // []
console.log(JSON.stringify(account)); // '{}'
console.log(account.getBalance()); // 100
// Still accessible if you know about Symbols:
const symbols = Object.getOwnPropertySymbols(account);
console.log(account[symbols[0]]); // 100
```
---
## Computed Method Names
Computed property syntax works with method shorthand for dynamically-named methods:
### Basic Computed Methods
```javascript
const action = 'greet';
const obj = {
[action]() {
return 'Hello!';
},
[`${action}Loudly`]() {
return 'HELLO!';
}
};
console.log(obj.greet()); // 'Hello!'
console.log(obj.greetLoudly()); // 'HELLO!'
```
### Computed Generator Methods
```javascript
const iteratorName = 'values';
const collection = {
items: [1, 2, 3],
*[iteratorName]() {
for (const item of this.items) {
yield item * 2;
}
}
};
console.log([...collection.values()]); // [2, 4, 6]
```
### Computed Async Methods
```javascript
const fetchName = 'fetchData';
const api = {
async [fetchName](url) {
const response = await fetch(url);
return response.json();
}
};
// api.fetchData('https://api.example.com/data')
```
---
## Computed Getters and Setters
You can combine computed property names with [getters and setters](/beyond/concepts/getters-setters):
```javascript
const prop = 'fullName';
const person = {
firstName: 'Alice',
lastName: 'Smith',
get [prop]() {
return `${this.firstName} ${this.lastName}`;
},
set [prop](value) {
const parts = value.split(' ');
this.firstName = parts[0];
this.lastName = parts[1];
}
};
console.log(person.fullName); // 'Alice Smith'
person.fullName = 'Bob Jones';
console.log(person.firstName); // 'Bob'
console.log(person.lastName); // 'Jones'
```
### Symbol-Keyed Accessors
```javascript
const _value = Symbol('value');
const validated = {
[_value]: 0,
get [Symbol.for('value')]() {
return this[_value];
},
set [Symbol.for('value')](v) {
if (typeof v !== 'number') {
throw new TypeError('Value must be a number');
}
this[_value] = v;
}
};
validated[Symbol.for('value')] = 42;
console.log(validated[Symbol.for('value')]); // 42
```
---
## Real-World Use Cases
### Form Field Handling
React and Vue state updates commonly use computed properties:
```javascript
// React-style form handler
function handleInputChange(fieldName, value) {
return {
[fieldName]: value,
[`${fieldName}Touched`]: true,
[`${fieldName}Error`]: null
};
}
const updates = handleInputChange('email', 'alice@example.com');
console.log(updates);
// {
// email: 'alice@example.com',
// emailTouched: true,
// emailError: null
// }
```
### Redux-Style State Updates
```javascript
// Reducer pattern with computed properties
function updateField(state, field, value) {
return {
...state,
[field]: value,
lastModified: Date.now()
};
}
const state = { name: 'Alice', email: '' };
const newState = updateField(state, 'email', 'alice@example.com');
console.log(newState);
// { name: 'Alice', email: 'alice@example.com', lastModified: 1699123456789 }
```
### Internationalization (i18n)
```javascript
function createTranslations(locale, translations) {
return {
[`messages_${locale}`]: translations,
[`${locale}_loaded`]: true,
[`${locale}_timestamp`]: Date.now()
};
}
const spanish = createTranslations('es', { hello: 'hola' });
console.log(spanish);
// {
// messages_es: { hello: 'hola' },
// es_loaded: true,
// es_timestamp: 1699123456789
// }
```
### Dynamic API Response Mapping
```javascript
function normalizeResponse(entityType, items) {
return items.reduce((acc, item) => ({
...acc,
[`${entityType}_${item.id}`]: item
}), {});
}
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' }
];
const normalized = normalizeResponse('user', users);
console.log(normalized);
// {
// user_1: { id: 1, name: 'Alice' },
// user_2: { id: 2, name: 'Bob' }
// }
```
---
## Common Mistakes and Edge Cases
### Duplicate Computed Keys: Last One Wins
When multiple computed properties evaluate to the same key, the last one overwrites previous values:
```javascript
const key = 'same';
const obj = {
[key]: 'first',
['sa' + 'me']: 'second',
same: 'third' // Static key, same string
};
console.log(obj); // { same: 'third' }
```
### Keys That Throw Errors
If the key expression throws, object creation is aborted entirely:
```javascript
function badKey() {
throw new Error('Key evaluation failed');
}
// This throws before the object is created
try {
const obj = {
valid: 'ok',
[badKey()]: 'never reached'
};
} catch (e) {
console.log(e.message); // 'Key evaluation failed'
}
```
### Object Keys: toString() Collisions
Objects used as keys call `toString()`, which can cause unexpected collisions:
```javascript
const objA = { toString: () => 'key' };
const objB = { toString: () => 'key' };
const data = {
[objA]: 'first',
[objB]: 'second' // Overwrites! Both β 'key'
};
console.log(data); // { key: 'second' }
```
### The `__proto__` Special Case
The `__proto__` key has special behavior depending on how it's written:
```javascript
// Non-computed: Sets the prototype!
const obj1 = { __proto__: Array.prototype };
console.log(obj1 instanceof Array); // true
console.log(Object.hasOwn(obj1, '__proto__')); // false
// Computed: Creates a normal property
const obj2 = { ['__proto__']: Array.prototype };
console.log(obj2 instanceof Array); // false
console.log(Object.hasOwn(obj2, '__proto__')); // true
// Shorthand: Also creates a normal property
const __proto__ = 'just a string';
const obj3 = { __proto__ };
console.log(obj3.__proto__); // 'just a string' (own property)
```
<Warning>
**Important:** Only the non-computed colon syntax (`__proto__: value`) sets the prototype. Computed `['__proto__']` and shorthand `{ __proto__ }` create regular properties.
</Warning>
---
## Key Takeaways
<Info>
**The key things to remember:**
1. **Computed properties use `[expression]` syntax** in object literals to create dynamic key names at runtime.
2. **The key expression is evaluated before the value expression.** Properties are processed left-to-right in source order.
3. **Non-string/Symbol keys are coerced via ToPropertyKey().** Numbers become strings, objects call `toString()`.
4. **Symbols can ONLY be used as keys via computed property syntax.** The syntax `{ mySymbol: value }` creates a string key `"mySymbol"`.
5. **Well-known Symbols customize object behavior.** Use `[Symbol.iterator]` for iteration, `[Symbol.toStringTag]` for type strings.
6. **Computed method syntax enables dynamic method names.** Works with regular methods, generators, and async methods.
7. **Computed getters/setters enable dynamic accessor properties.** Combine `get [expr]()` and `set [expr](v)` for dynamic accessors.
8. **Pre-ES6 required two steps; ES6 enables single-expression objects.** This is especially useful in `reduce()`, arrow functions, and default parameters.
9. **Duplicate computed keys are allowedβlast one wins.** No error is thrown; the later value simply overwrites.
10. **The `__proto__` key behaves differently in computed vs non-computed form.** Only non-computed colon syntax sets the prototype.
</Info>
---
## Test Your Knowledge
<AccordionGroup>
<Accordion title="What's the difference between { key: value } and { [key]: value }?">
**Answer:**
- `{ key: value }` creates a property with the literal name `"key"` (a static string).
- `{ [key]: value }` evaluates the variable `key` and uses its **value** as the property name.
```javascript
const key = 'dynamicName';
const static = { key: 'value' };
console.log(static); // { key: 'value' }
const dynamic = { [key]: 'value' };
console.log(dynamic); // { dynamicName: 'value' }
```
The square brackets signal "evaluate this expression to get the key name."
</Accordion>
<Accordion title="In what order are key and value expressions evaluated?">
**Answer:**
The **key expression is evaluated first**, then the **value expression**. This happens for each property in left-to-right order.
```javascript
let n = 0;
const obj = {
[++n]: n, // key: 1, value: 1
[++n]: n // key: 2, value: 2
};
// { '1': 1, '2': 2 }
```
The `++n` in the key runs before `n` in the value is read, so they match.
</Accordion>
<Accordion title="What happens when you use an object as a computed key?">
**Answer:**
The object is converted to a string via its `toString()` method. By default, this returns `"[object Object]"`, which can cause unintended collisions:
```javascript
const a = { id: 1 };
const b = { id: 2 };
const obj = {
[a]: 'first',
[b]: 'second' // Overwrites! Both β "[object Object]"
};
console.log(obj); // { '[object Object]': 'second' }
```
Custom `toString()` methods can provide unique keys, but this pattern is error-prone. Use Symbols or string IDs instead.
</Accordion>
<Accordion title="Why must Symbol keys use computed property syntax?">
**Answer:**
The shorthand and colon syntax only accept identifiers or string literals as property names. Writing `{ mySymbol: value }` creates a property named `"mySymbol"` (a string), not a Symbol-keyed property.
```javascript
const sym = Symbol('id');
const wrong = { sym: 'value' };
console.log(Object.keys(wrong)); // ['sym'] - string key!
const right = { [sym]: 'value' };
console.log(Object.keys(right)); // [] - Symbol key is hidden
console.log(Object.getOwnPropertySymbols(right)); // [Symbol(id)]
```
The `[sym]` syntax tells JavaScript to evaluate the variable and use the Symbol itself as the key.
</Accordion>
<Accordion title="How do you create a dynamically-named method?">
**Answer:**
Use computed property syntax with method shorthand:
```javascript
const action = 'processData';
const handler = {
[action](data) {
return data.map(x => x * 2);
},
// Generator method
*[`${action}Iterator`](data) {
for (const item of data) {
yield item * 2;
}
},
// Async method
async [`${action}Async`](url) {
const response = await fetch(url);
return response.json();
}
};
console.log(handler.processData([1, 2, 3])); // [2, 4, 6]
```
This works with regular methods, generators (`*[name]()`), and async methods (`async [name]()`).
</Accordion>
<Accordion title="What happens with duplicate computed keys?">
**Answer:**
Duplicate keys are allowedβthe **last one wins** and overwrites previous values. No error is thrown:
```javascript
const obj = {
['x']: 1,
['x']: 2,
x: 3
};
console.log(obj); // { x: 3 }
```
This applies whether the duplicate comes from computed properties, static properties, or a mix. The same rule applies to the rest of JavaScriptβlater assignments overwrite earlier ones.
</Accordion>
</AccordionGroup>
---
## Related Concepts
<CardGroup cols={2}>
<Card title="Modern JS Syntax (ES6+)" icon="wand-magic-sparkles" href="/concepts/modern-js-syntax">
Overview of ES6+ features including destructuring, spread, arrow functions, and enhanced object literals.
</Card>
<Card title="JavaScript Type Nuances" icon="code" href="/beyond/concepts/javascript-type-nuances">
Deep dive into Symbols, a primary use case for computed property keys in JavaScript.
</Card>
<Card title="Getters & Setters" icon="arrows-rotate" href="/beyond/concepts/getters-setters">
Combine computed property names with get and set for dynamic accessor properties.
</Card>
<Card title="Property Descriptors" icon="sliders" href="/beyond/concepts/property-descriptors">
Control writable, enumerable, and configurable flags on your computed properties.
</Card>
<Card title="Object Methods" icon="cube" href="/beyond/concepts/object-methods">
Iterate and transform objects using Object.keys(), entries(), and fromEntries().
</Card>
<Card title="Tagged Template Literals" icon="wand-magic-sparkles" href="/beyond/concepts/tagged-template-literals">
Another ES6+ syntax feature for advanced string processing with template literals.
</Card>
</CardGroup>
---
## References
<CardGroup cols={2}>
<Card title="Object Initializer β MDN" icon="book" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#computed_property_names">
Official MDN reference for object literals with a dedicated section on computed property names.
</Card>
<Card title="Property Accessors β MDN" icon="book" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_accessors">
Understand bracket notation, the foundation for how computed property names work.
</Card>
<Card title="Symbol β MDN" icon="book" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol">
Comprehensive reference on Symbols, commonly used with computed property syntax.
</Card>
<Card title="Working with Objects β MDN" icon="book" href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_objects">
Beginner guide covering object fundamentals and property access patterns.
</Card>
</CardGroup>
---
## Articles
<CardGroup cols={2}>
<Card title="Objects β javascript.info" icon="newspaper" href="https://javascript.info/object#computed-properties">
Excellent tutorial with a dedicated "Computed properties" section and interactive examples.
</Card>
<Card title="ES6 In Depth: Symbols" icon="newspaper" href="https://hacks.mozilla.org/2015/06/es6-in-depth-symbols/">
Mozilla Hacks article explaining Symbols and their use as computed property keys for iterables.
</Card>
<Card title="Exploring ES6: New OOP Features" icon="newspaper" href="https://exploringjs.com/es6/ch_oop-besides-classes.html">
Dr. Axel Rauschmayer's deep technical analysis of computed property keys and ES6 object enhancements.
</Card>
<Card title="Computed Property Names" icon="newspaper" href="https://ui.dev/computed-property-names">
Focused practical article with before/after ES6 comparisons and real-world examples.
</Card>
</CardGroup>
---
## Videos
<CardGroup cols={2}>
<Card title="ES6 JavaScript Tutorial" icon="video" href="https://www.youtube.com/@TraversyMedia">
Traversy Media's comprehensive ES6 coverage including enhanced object literals and computed properties.
</Card>
<Card title="Modern JavaScript Tutorial" icon="video" href="https://www.youtube.com/@NetNinja">
The Net Ninja's series on modern JavaScript features with clear explanations of ES6 syntax.
</Card>
<Card title="JavaScript ES6 Features" icon="video" href="https://www.youtube.com/@WebDevSimplified">
Web Dev Simplified tutorials explaining ES6 features including object shorthand and computed properties.
</Card>
<Card title="JavaScript Quick Tips" icon="video" href="https://www.youtube.com/@Fireship">
Fireship's fast-paced explainers covering JavaScript syntax features and best practices.
</Card>
</CardGroup>