-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaccess_object_properties.js
More file actions
29 lines (21 loc) · 940 Bytes
/
access_object_properties.js
File metadata and controls
29 lines (21 loc) · 940 Bytes
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
// What is an object property?
const object = {
propertyOne: 'one',
propertyTwo: 'two',
propertyThree: 'three',
}
// There are two ways to access the properties
// 1. Dot notation
const one = object.propertyOne; // Accessing 'propertyOne' using dot notation
const two = object.propertyTwo; // Accessing 'propertyTwo' using dot notation
console.log({ one, two });
// 2. Square bracket notation
const oneSq = object['propertyOne']; // Accessing 'propertyOne' using square bracket notation
const twoSq = object['propertyTwo']; // Accessing 'propertyTwo' using square bracket notation
console.log({ oneSq, twoSq });
// You can also pass in any expression to square brackets
const x = object[false ? 'propertyOne' : 'propertyThree']; // Using an expression in square brackets
console.log({ x });
const someVariable = 'propertyTwo';
const y = object[someVariable]; // Using a variable to access a property
console.log({ y });