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?
0 - An object is a collection of key-value pairs, where each key is a string or symbol and each value can be any data type. An array, on the other hand, is an ordered collection of values, where each value can be of any data type, and the values are accessed by index number.
1- In JavaScript, const is used to create a variable that cannot be reassigned. When used with objects, the object itself cannot be reassigned to a new value, but the properties of the object can still be modified.
2 - Bracket syntax and dot syntax are used to access properties of an object. Bracket syntax is used when the property name is a variable or is not known until runtime. Dot syntax is used when the property name is known and is a valid identifier.
Example of something that works with bracket syntax but not dot syntax:
Copy code
const obj = {
"property name": "value"
}
console.log(obj["property name"]) // "value"
console.log(obj.property name) //error
Example of something that works with dot syntax but not bracket syntax:
Copy code
const obj = {
propertyName: "value"
}
console.log(obj.propertyName) // "value"
console.log(obj["propertyName"]) // "value"
3 - Computed properties are properties that are defined using expressions, rather than fixed property names. This allows for dynamic property names that are calculated at runtime.
function createObject(key, value) {
return {
[key]: value
}
}
console.log(createObject("propertyName", "value")) // {propertyName: "value"}
============================================
let fruit = prompt("Which fruit to buy?", "apple");
let bag = {
[fruit]: 5, // the name of the property is taken from the variable fruit
};
alert( bag.apple ); // 5 if fruit="apple"
4 - Object.keys() is used to return an array of the object's own enumerable property names, whereas Object.entries() is used to return an array of the object's own enumerable property [key, value] pairs.
Copy code
const obj = {
key1: "value1",
key2: "value2"
}
console.log(Object.keys(obj)) // ["key1", "key2"]
console.log(Object.entries(obj)) // [["key1", "value1"], ["key2", "value2"]]
5 - To get only the values of an object, you can use the Object.values() method which returns an array of a given object's own enumerable property values, in the same order as that provided by a for...in loop.
Copy code
const obj = {
key1: "value1",
key2: "value2"
}
console.log(Object.values(obj)) // ["value1", "value2"]