Skip to content

Instantly share code, notes, and snippets.

@tech-chieftain
Forked from halitbatur/discussion.md
Created October 8, 2022 08:23
Show Gist options
  • Save tech-chieftain/f35b43e1adb094a262422c9c222811e1 to your computer and use it in GitHub Desktop.
Save tech-chieftain/f35b43e1adb094a262422c9c222811e1 to your computer and use it in GitHub Desktop.
Object discussion questions

Object Discussion Questions

There is some coding in this discussion. Feel free to write them in a REPL or in the comments below.

  1. How is an object different from an array?
  2. How does const work with objects?
  3. 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.
  4. What are computed properties? Write an example code.
  5. What is the difference between Object.keys and Object.entries? Write example code using both of them.
  6. How do we get only the values of an object?
@kogunlu
Copy link

kogunlu commented Oct 8, 2022

0. Arrays stores a list of data as one variable. Objects store values with their keys.
const arr = ["abc", "abc"]
const obj = {key: "abc", key2: "abc}
1. Even if the Object is const, we can change, add or delete the elements inside the objects.
2. Bracket notation allows you to access properties with special characters in their names, while you can not do this with dot notation.
i.e. we can not use names with space: object.element value but we can use object["element value"]
while coding when you put dot(.) after the name of the object, the list of the possible elements will appear while when you use brackets ([]) it won't show up.
3. Allows you to put an expression in brackets [] , that will be computed and used as the property name
i.e.
let propName = 'c';

const rank = {
a: 1,
b: 2,
[propName]: 3,
};

console.log(rank.c); // 3

4. While Object.keys shows only the keys of the elements, Object.entries shows both keys and values.
5. Object.values

@halilibrahimcelik
Copy link

0.How is an object different from an array?

Object is a data type
array is also an object.
Object has properties, each keys can store boolean, strings or numbers, anything we want.
main differences are;
=> Object has key value pairs features that allows us to define specific values for each keys.
=>Objects represent “things” with characteristics (aka properties), while arrays create and store lists of data in a single variable.

1.How does const work with objects?
we use const with the object if we do not want to change the value once that variable has been initialized.

const person={
age:30,
name:"Halil",

}
//Dot notation
person.name="Halil",

//Bracket notation
person["name"]="Halil",
key differences
Bracket notation allows you to access properties with special characters in their names, while you can not do this with dot notation.

3.What are computed properties ?
Computed properties allow you to use the values of expressions as property names of an object.

@dilaracetinberk
Copy link

  1. Object is a data type, array is an object as well. Object has properties like boolean,strings etc.
  2. we use const with the object if we don't want to change the value which is certain.
  3. Sometimes it is impossible to access those properties with dot notation. wildKeys.'string' ;
    3

@caglabircan
Copy link

caglabircan commented Oct 8, 2022

  1. Objects can store pairs (keys and values). Arrays only store values.
  2. Const creates a binding so we can't use the same name with another object.
  3. Dot notation is easier to read and write. Cheat: Square bracket notation allows access to properties containing special characters and selection of properties using variables.

4.The Object.keys() function returns an array of the object's own properties. Object.entries() function returns an array of entries.
5.Object.values

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment