Changing the key:value pairs in Javascript

Recently, I was looking at how to change the key:value pairs in JavaScript. I know that you can change the pairs directly with dot or bracket notation. Or you could change them by making a function and then passing your object into your function as a parameter when you call the function. Could anyone tell me why we can change the key:value pairs with a function, rather than just changing it directly? It seems to me that changing a pair directly is easier to write and understand than a function.

the function might do validation, the value could come from the user and might need validating (minimum length for example) before the value is added to the dictionary/object.

the function could also eliminate duplicate code.

you will run more into this issue during the lessons, a certain concept might seem a bit overkill/abstract but can be useful later when the program you are building might become more complex.

2 Likes

When a program is running we do not have manual access to the code or data structures. That’s why the language has built in functions that allow the program to manipulate those objects dynamically and on the fly.

JavaScript is the engine that permits dynamic manipulation not only of the data objects, but of the DOM, as well. The program has to be able to respond to individual user events and inputs. We cannot do it any other way.

1 Like