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.