YogeshChauhan . com

Accessing and Setting features of JavaScript Objects

in JavaScript on 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


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

Styling Lists with CSSCSSWhat are Null Sessions?MiscHow to define constants in PHP?PHPHow to import MySQL small sample database into phpMySQL?SQL/MySQLHow to create a simple slider with CSS and jQuery?CSSObservation of Human Behavior [Shopping Observation Example]MiscHow to Secure Web or Mobile Browsers?MiscHow to change CSS with jQuery?CSSWordPress: How to print ACF array field values?WordPressSolution for Xcode 11 Command PhaseScriptExecution failed with a nonzero exit code errorMiscHow to position an image on top of another image using CSS?CSSWhat is Prototypal Inheritance in JavaScript?JavaScript