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?
@afrakucukaydin @MOHAMMAD-ALMOHAMMAD @Abd2023 @tomiece317
0. arrays are objects, but objects contain key values pairs, and arrays contain index based values in an ordered fashion, though arrays can also contain key value pairs. Objects are accessed using keys, while arrays are accessed using indices.
const can be used to declare a variable that cannot be reassigned, but it does not make the variable immutable. When an object is declared with const, the reference to the object cannot be reassigned to a different object. However, the properties of the object can still be modified. This means that while the object itself is considered constant, its properties can still be changed.
bracket syntax and dot syntax are two ways to access and manipulate object properties.Something that works with bracket syntax but not dot syntax is accessing object properties with spaces or special characters in their names. For example: myObj['my property'] would work with bracket syntax but not with dot syntax (myObj.my property is not valid). Something that works with dot syntax but not bracket syntax is accessing object properties with literal identifiers. For example: myObj.myProperty would work with dot syntax but not with bracket syntax (myObj.'myProperty' is not valid).
Computed properties allow you to dynamically compute the property names in an object literal.
const propName='gg';
const obj={ [propName]:3507};
console.log(obj.gg); //output will be : 3507
we use Object.keys() and Object.entries() to get info about the properties in an object. Object.keys returns the property names. Object.enrties returns the entries in an array form.
const obj={a:1, b:2, c:3};
console.log(Object.keys(obj)); // output will be: ["a","b","c"]
console.log(Object.entries(obj)); // output will be: [["a", 1], ["b", 2] , ["c", 3]]
We can get only the values of an object by using the Object.values() method. It returns an array of values from the object.