There is some coding in this discussion. Feel free to write them in a REPL or in the comments below.
- How is an object different from an array?
- How does
const
work with objects? - Explain the difference between bracket syntax and dot syntax. Give an example of something that works with bracket syntax but not dot syntax. Give an example of something that works with dot syntax but not bracket syntax.
- What are computed properties? Write an example code.
- What is the difference between
Object.keys
andObject.entries
? Write example code using both of them. - How do we get only the values of an object?
How is an object different from an array?
Objects represent a special data type that is [mutable] and can be used to store a collection of data (rather than just a single value).
Arrays are a special type of variable that is also mutable and can also be used to store a list of values.
How does const work with objects?
The const keyword makes a variable itself immutable, not its assigned content. When the content is an object, this means the object itself can still be altered.
Therefore, it's possible to change the content of the object that is declared with const variable, but you cannot assign a new object to a const variable.
Explain the difference between bracket syntax and dot syntax. Give an example of something that works with bracket syntax but not dot syntax. Give an example of something that works with dot syntax but not bracket syntax.
When you use dot notation, key means the actual property in the object, which will not be there. So, undefined is returned which is not equal to true.
When you use [] notation you are accessing the property in the object with the name in the variable key. So, that will work.
For example,
This will print,
What are computed properties? Write an example code.
allows you to use an expression in brackets []. It’ll then use the result of the expression as the property name of an object.
For example:
What is the difference between Object.keys and Object.entries? Write example code using both of them.
[Object.keys] returns only the own property names.
[Object.entries] returns an array of arrays with key and value.
How do we get only the values of an object?
To access the properties of an object without knowing the names of those properties you can use a for ... in loop: