-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject.js
More file actions
20 lines (18 loc) · 816 Bytes
/
Copy pathobject.js
File metadata and controls
20 lines (18 loc) · 816 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let name = 'rajat';
let age = 21;
let city = 'delhi';
// let obj = {
// name: name,
// age: age,
// city: city
// }
let obj = {name, age, city}; //in modern javascript(ES6) we can write like this if key and value both are same
// let obj = {name, age, city,key:11}; //we can also add key value pair like this for line 18
console.log(obj);
// console.log(obj.age); //to access value of key age in obj
// console.log(obj['city']); //another way to access value of key city in obj
for(let key in obj){ //to iterate all key value pair in object
console.log(key);
// console.log(obj.key); //undefined because obj.key means key is key name but here key is variable which contain key name
console.log(obj[key]); //to access value of key in object by variable key
}