Calling object properties by string | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Calling object properties by string

Hello! I have following code: const obj = { a: "blablabla", b: "bloblo", c: "ble" }; I want to get an property of this object by typing its name: const get = "a"; console.log(obj[get]); It works, but my question is: is that a proper way to get object properties? I have read in many courses that this way of calling is proper for calling array elements, but not objects properties. Object properties we get from this syntax: object.property. Thank you in advance for anserws, bye!

15th Aug 2017, 6:20 AM
Grz
Grz - avatar
4 Answers
+ 1
thank you for answer, but i need to call it by string variable (from user input for example)
15th Aug 2017, 7:12 PM
Grz
Grz - avatar
+ 1
because i can't call it by obj.get in my example, i always need obj[get]
16th Aug 2017, 4:00 AM
Grz
Grz - avatar
16th Aug 2017, 9:34 AM
Grz
Grz - avatar
0
thank you, you make it clear to me :-)
17th Aug 2017, 10:38 AM
Grz
Grz - avatar