Updating/adding/deleting new properties

Good morning Campers,

I’m wondering why to update/add new properties to objects in JS we have to use the bracket notation or dot notation, and we need to use the delete keyword to delete a property from an object. Why can’t we just modify the property directly inside of an object instead of modifying its data outside of the scope of the object?

Thanks,
Mario

it depends if you need static object (set like they before the code execute and never changing) or dynamic object, changing through the execution of the code, maybe depending on what the user does (in interactive web pages) or in calculations. It could also be that your code get the object from an API, and at that point you can’t certainly change the original object, but you need to do something with the data received anyway, so you are stuck with object manipulation of this kind

1 Like

Can you give an example? Most usage of Objects in JS is as what would be called maps/records/dicts in other languages, and I’m unsure of how what you’re saying could possibly work

I was just wondering why do it after we already declared the object and not just do the changes directly in the object. @ilenia gave a pretty good explanation and I’m more clarified on this now. :slight_smile:

1 Like