To add a property to an object in JavaScript, you can use dot notation or bracket notation. Here's an example using dot notation:
index.tsx194 chars11 lines
In this example, we define an object obj
with two properties: name
and age
. To add a new property called city
, we simply use dot notation and assign a value to it ('New York'
in this case).
Alternatively, you can use bracket notation to add a new property to an object:
index.tsx201 chars11 lines
In this example, we use bracket notation and the property name is enclosed in quotes. The effect is the same as when using dot notation.
gistlibby LogSnag