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?
Room 12
Contributors :
@Silvor23
@abdulsalamhamandoush
@motaz99
0- Arrays are indexed base, and we have pair key values in objects. (Array is also a type of an object in JS). Arrays are useful when dealing with ordered data, while objects are more flexible and can be used to represent more complex data structures such as maps, dictionaries, and records.
1- Const prevents the variable from being reassigned to a new object, but it does not make the object immutable.
2- dot notation is used while we have strict properties and bracket notations are used while our property is dynamic or not a valid identifier.
const person = {
name: 'John',
age: 30
};
const propertyName = 'name';
console.log(person[propertyName]);
// dot notation exam plcosnole.log(person.name)
3- Computed properties are a way to dynamically generate object property names. They allow us to use an expression, enclosed in square brackets, to compute the property name at runtime. This is particularly useful when the property name cannot be determined until runtime.
};
console.log(user['user_name']); // Output: John
console.log(user['user_age']); // Output: 30
4- both methods in JavaScript to access the properties of an object,
Object.keys() returns an array of all the enumerable properties of an object they appear in the object. Each element of the array is a string that represents a property
example :
const person = {
name: 'John',
age: 30,
gender: 'Male'
};
const keys = Object.keys(person);
console.log(keys); // Output: ['name', 'age', 'gender']
const entries = Object.entries(person);
console.log(entries); // Output: [['name', 'John'], ['age', 30], ['gender', 'Male']]
5- We can use Object.values() and we give this function the object that we want to take it's value out, for example
const obj = {
a: 'Abdulsalam', b: 'Rasam', c: 'Motaz', d: 'Abdullah'}
const Values = Object.values(obj)
console.log(values)