Skip to content

Instantly share code, notes, and snippets.

@tech-chieftain
Created January 13, 2023 20:02
Show Gist options
  • Save tech-chieftain/0e3fd5fcbe3e337883bdb38584971239 to your computer and use it in GitHub Desktop.
Save tech-chieftain/0e3fd5fcbe3e337883bdb38584971239 to your computer and use it in GitHub Desktop.
Objects Discussion

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?
@Haneen-Abdulgllil
Copy link

How is an object different from an array?

  • Objects represent a special data type that is [mutable] and can be used to store a collection of data (rather than just a single value).

  • Arrays are a special type of variable that is also mutable and can also be used to store a list of values.

How does const work with objects?

The const keyword makes a variable itself immutable, not its assigned content. When the content is an object, this means the object itself can still be altered.
Therefore, it's possible to change the content of the object that is declared with const variable, but you cannot assign a new object to a const variable.

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.

When you use dot notation, key means the actual property in the object, which will not be there. So, undefined is returned which is not equal to true.
When you use [] notation you are accessing the property in the object with the name in the variable key. So, that will work.
For example,

var myObj = {
    myVar : 1
};

for (var key in myObj) {
    console.log(key);
    console.log(myObj.key);
    console.log(myObj[key]);
}

This will print,

myVar
undefined
1

What are computed properties? Write an example code.

allows you to use an expression in brackets []. It’ll then use the result of the expression as the property name of an object.
For example:

let Name = 'haneen';

const rank = {
  a: 1,
  b: 2,
  [Name]: 6,
};

console.log(rank.haneen); // 6

What is the difference between Object.keys and Object.entries? Write example code using both of them.

[Object.keys] returns only the own property names.

[Object.entries] returns an array of arrays with key and value.

 let user = {
 name: "haneen",
 age: 29
};

Object.keys(user) = ["name", "age"]

Object.entries(user) = [ ["name","haneen"], ["age",29] ]

How do we get only the values of an object?

To access the properties of an object without knowing the names of those properties you can use a for ... in loop:

for(key in data) {
    if(data.hasOwnProperty(key)) {
        var value = data[key];
        //do something with value;
    }
}

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