Skip to content

Commit 09ad812

Browse files
committed
Nested Objects, Bracket Notation
1 parent 95eebb8 commit 09ad812

File tree

1 file changed

+23
-17
lines changed

1 file changed

+23
-17
lines changed

app.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,42 @@
1-
// Objects Basics
1+
// nested objects
2+
// set variable as property value
3+
// dot notation vs bracket notation
24

3-
/*
4-
Objects are collections of key (property) value pairs
5-
Property values can be strings, numbers, booleans, arrays and functions however if the property value is a function it's called a method
6-
*/
5+
const age = 30;
76

8-
// object literal syntax,{}
9-
// dot notation
7+
let random = "random value";
8+
random = "age";
109

1110
const person = {
1211
name: "john",
13-
age: 25,
12+
age: age,
1413
siblings: ["anna", "susan", "peter"],
1514
greet: function (name) {
1615
console.log(`Hi, I am ${name}`);
1716
},
1817
sayHello(name) {
1918
console.log(`Hello, My name is ${name}`);
2019
},
20+
job: {
21+
title: "developer",
22+
company: {
23+
name: "coding addict",
24+
address: "123 main street",
25+
},
26+
},
27+
"random-value": "random",
2128
};
2229

23-
console.log(person.name);
30+
console.log(person.job.company.name);
31+
32+
console.log(person.work); // undefined
2433

25-
// aassign and access the variable
26-
const firstName = person.sayHello("john");
34+
/* Cannot read properties of undefined (reading 'title') */
35+
// console.log(person.work.title);
2736

28-
// Adding property
29-
person.city = "illinois";
37+
console.log(person["random-value"]);
3038

31-
//Deleting property
32-
// delete person.siblings;
33-
const deleteSiblings = delete person.siblings;
34-
console.log(deleteSiblings);
39+
// check in person object for the property with age which is value of "random"
40+
console.log(person[random]);
3541

3642
console.log(person);

0 commit comments

Comments
 (0)