Languages

JavaScript - create object property from variable value

0 points
Asked by:
leila
435

How can I create object property from variable value?

I want to add a new property to myObject, name it key1 and give it a value of value1 but when I do this it returns undefined:

1 answer
0 points
Answered by:
Admin Dirask Community
4380

There are two types of property accessors in JavaScript: dot notation and bracket notation.

Use bracket notation to create object property from the variable value:

Practical example

Alternative solution (ES6+)

 

References

  1. Property accessors - JavaScript | MDN
0 commentsAdd comment
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join