Accessing and Setting features of JavaScript Objects
December 30, 2020

Property accessors provide access to an object’s properties by using the dot notation or the bracket notation.

const person1 = {};
person1['firstname'] = 'Yogesh';
person1['lastname'] = 'Chauhan';
// output: "Yogesh"

const person2 = {
  firstname: 'Yog',
  lastname: 'Chauhan'
// output: "Chauhan"

We can access features/properties in JavaScript using the following different ways.

object.property => dot notation
object['property'] => bracket notation

Lets try to add a property to the person1 object using bracket operator.

person1['middlename'] = "Yogi";



{ firstname: 'Yogesh', lastname: 'Chauhan', middlename: 'Yogi' }

We can add the same property to the person1 object using dot operator as well.

person1.middlename = "Yogi";



{ firstname: 'Yogesh', lastname: 'Chauhan', middlename: 'Yogi' }

Credit: MDN Docs


Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 4 How to use data-* Attributes in HTML? Mar 4 The substr() method in JavaScript and how it’s different from substring() Mar 4 A complete guide to add responsive YouTube videos using HTML and CSS Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript

You might also like these

SELF JOIN in PostgresPostgresNumber Properties in JavaScriptJavaScriptCan We Use For Loop to Loop Through Associative Arrays in PHP?PHPHow to animate list items using CSS and JavaScript?CSSHow to display modal from bottom to top using CSS and JavaScript?CSSSolution to “TypeError: ‘x’ is not iterable” in Angular 9AngularPHP Login System using PDO Part 2: Login using Email or UsernamePHPPre-defined DatePipe format options in Angular 9AngularHow to Write Complex Search Queries in SQL?SQL/MySQLHow to remove border from :visited image, link or button?CSSExplanation of PostgreSQL PgAdmin interfacePostgresHow to make a web page look good on any device?HTML