-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript2.js
More file actions
43 lines (35 loc) · 1.36 KB
/
script2.js
File metadata and controls
43 lines (35 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Example user profile object
const userProfile = {
id: 123,
name: 'John Doe',
address: {
street: '123 Main St',
city: 'Anytown',
// state: 'CA', // This property might be missing
country: 'USA',
postalCode: '12345'
},
preferences: {
theme: 'dark',
notifications: {
email: true,
sms: false
}
},
// getFullRecord: function() {
// return `${this.name}, ${this.address.city}, ${this.address.country}`;
// }
};
// Accessing nested properties safely using optional chaining
const userState = userProfile.address?.state;
const emailNotifications = userProfile.preferences?.notifications?.email;
const smsNotifications = userProfile.preferences?.notifications?.sms;
// Output the results
console.log(`User State: ${userState}`); // Output: User State: undefined
console.log(`Email Notifications: ${emailNotifications}`); // Output: Email Notifications: true
console.log(`SMS Notifications: ${smsNotifications}`); // Output: SMS Notifications: false
let p = "postalCode";
console.log(userProfile.address?.[p]);
// console.log(userProfile.getFullRecord); // undefined
// console.log(userProfile.getFullRecord()); // TypeError: userProfile.getFullRecord is not a function
console.log(userProfile.getFullRecord?.()); // undefined