Solution: In javascript, the object is almost everything. If you understand the objects, you understand javascript.
- Booleans can be objects if they defined with a new keyword
- Strings can be objects if they defined with a new keyword
- Numbers can be objects if they defined with a new keyword
- Arrays are always objects
- Functions are always objects
- Dates are always objects
- Regular expressions are always objects
- Maths are always objects
All javascript values except primitive are objects.
Basically, Javascript objects are key-value pairs and We can add key-value pairs in multiples ways but here I will explain the most popular 4 ways:-
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