How many ways can a property of a JavaScript object be accessed?

JavascriptObject Oriented ProgrammingFront End Technology

 An object property can be accessed in two ways. One is .property and the other is [property].

Syntax-1

Object.property;

Syntax-2

Object["property"];

For better understanding, lets' look at the following example.

In the following example an object called 'person' is defined and its properties were accessed in a dot notation.

Example

Live Demo

<html>
<body>
<script>
var person = {
   firstname:"Ram",
   lastname:"kumar",
   age:50,
   designation:"content developer"
};
document.write(person.firstname + " " + "is in a role of" + " " + person.designation);
</script>
</body>
</html>

Output

Ram is in a role of content developer

In the following example the properties of an object 'person' were accessed in bracket notation.

Example

Live Demo

<html>
<body>
<script>
var person = {
   firstname:"Ram",
   lastname:"kumar",
   age:50,
   designation:"content developer"
};
document.write(person['firstname']+ " " + "is in a role of" + " " + person['designation']);
</script>
</body>
</html>

Output

Ram is in a role of content developer
raja
Published on 26-Jun-2019 16:23:45
Advertisements