Categories
JavaScript Answers

How to create a function returning an object with JavaScript?

Spread the love

Sometimes, we want to create a function returning an object with JavaScript.

In this article, we’ll look at how to create a function returning an object with JavaScript.

How to create a function returning an object with JavaScript?

To create a function returning an object with JavaScript, we can make a function that returns an object literal.

For instance, we write

const makeGamePlayer = (name, totalScore, gamesPlayed) => {
  return {
    name,
    totalscore,
    gamesPlayed,
  };
};

to define the makeGamePlayer function that returns an object with the name, totalScore, and gamesPlayed properties.

Conclusion

To create a function returning an object with JavaScript, we can make a function that returns an object literal.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.