Categories
JavaScript Answers

How to put an array inside a JavaScript object?

Spread the love

Sometimes, we want to put an array inside a JavaScript object.

In this article, we’ll look at how to put an array inside a JavaScript object.

How to put an array inside a JavaScript object?

To put an array inside a JavaScript object, we set its as a property’s value.

For instance, we write

const defaults = {
  backgroundcolor: "#000",
  color: "#fff",
  weekdays: ["sun", "mon", "tue", "wed", "thu", "fri", "sat"],
};

to set the default.weekdays property to the ["sun", "mon", "tue", "wed", "thu", "fri", "sat"] array.

Conclusion

To put an array inside a JavaScript object, we set its as a property’s value.

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 *