Solution: In javascript, the object is almost everything. If you understand the objects, you understand javascript.
All javascript values except primitive are objects.
1) Using Dot Notation:
We can easily add key-value pairs using dot notation, like this:
2) Using Square bracket:
Dot notation has some limitations. If keys are dynamic or if it starts with a number or if it contains spaces or hyphens, you will get an error. So in that case, we can use a Square bracket method:
3) Using Spread(...) operator:
The Spread operator creates a new object and it is useful when you don't want to change the original object.
4) Using Object.assign() method:
This method takes two parameters, the first is the target object, and the second is the key-value pairs that you want to add to the target object(called source object). This method changes the original object i.e. Object.assign method does not create a new object.
For example:
0 Comments
Thank you for Comment