Languages
[Edit]
EN

JavaScript - access object properties

0 points
Created by:
Walter
466

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:

// ONLINE-RUNNER:browser;

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

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
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