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?
Answer 1
An object is a collection of key-value pairs, while an array is an ordered list of values
Objects are more flexible than arrays
Answer 2
it prevents the object from being reassigned to a different value. but the properties of the object can still be changed
Answer 3
Bracket syntax is used to access properties of an object using a string.
Answer 4
Computed properties are properties in JavaScript objects that are dynamically calculated based on other properties. They are useful for creating dynamic objects that can be used in a variety of situations.
Answer 5
Object.keys returns an array of a given object's own enumerable property names, whereas Object.entries returns an array of a given object's own enumerable string-keyed property [key, value] pairs.
Example code using Object.keys:
const person = {
name: 'John',
age: 25,
job: 'engineer'
};
const keys = Object.keys(person); // returns ['name', 'age', 'job']
console.log(keys);
Example code using Object.entries:
const person = {
name: 'John',
age: 25,
job: 'engineer'
};
const entries = Object.entries(person); // returns [['name', 'John'], ['age', 25], ['job', 'engineer']]
console.log(entries);
Answer 6
To get only the values of an object, you can use the Object.values() method. This method returns an array containing all the values of the object's own enumerable properties.