Why the first 'foo' is showing undefined as output? Would u pls help me to solve out the beneath problem? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
0

Why the first 'foo' is showing undefined as output? Would u pls help me to solve out the beneath problem?

var i = 0; var a = { ['foo' + i++]: i, ['foo' + ++i]: i, ['foo' + ++i]: i }; console.log(a.foo1); // 1 console.log(a.foo2); // 2 console.log(a.foo3); // 3

11th Jan 2023, 8:48 AM
Sony
Sony - avatar
1 Réponse
+ 3
Put this line after definition for <a> console.log( JSON.stringify( a ) ) You'll see that <a> doesn't have any member named 'foo1'. (Edit) In case you're wondering, why it has 'foo0', 'foo2' and 'foo3' members, but not 'foo1' - then know that the difference was due to the use of post and pre increment operator, which modifies value of <i> in different manner.
11th Jan 2023, 9:10 AM
Ipang