Categories
JavaScript Answers

How to add named properties to a JavaScript array as if it were an object?

Spread the love

To add named properties to a JavaScript array as if it were an object, we can add them directly to the array with assignment.

For instance, we write

const myArray = [];
myArray.a = "Athens";

to define the myArray array and assign the a property of the array to 'Athens'.

We can do this because arrays are objects.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *