Languages
[Edit]
EN

JavaScript - access object properties

0 points
Created by:
Walter
226

In this article, we would like to show you how to access object properties in JavaScript.

Quick solution:

objectName.property;

or 

objectName['property'];

or

const { property } = objectName;

 

Practical example

In this example, we access the user object properties using two methods:

  1. property key followed by the dot (.username),
  2. square brackets with the property key name as a string (['age']),
  3. object destructuring ({ country } = user).

Runnable example:

const user = {
    username: 'Tom',
    age: 23,
    country: 'USA'
}

const username = user.username;     // dot property accessor
const age = user['age'];            // square brackets property accessor
const { country } = user;           // object destructuring

console.log(username);      // Tom
console.log(age);           // 23
console.log(country);       // USA

Output:

Tom
23
USA

See also

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join