In Depth - JavaScript Factory Functions

JavaScript Factory Functions with ES6+
By Eric Elliott

A factory function is any function which is not a class or constructor that returns a (presumably new) object. In JavaScript, any function can return an object. When it does so without the new keyword, it’s a factory function.

1 Like

interesting! hmm so both pretty much do the same thing then just different syntax?

Very close to the same since both return new objects. Mind, any function in JS can return an object. I haven’t explored very deep down the rabbit hole since I tend to hover around beginner topics and syntax policing, naive versus advanced techniques (the latter of which is my weaker point) so cannot argue for or against either method.

Here is another article by the same author from three years ago…

JavaScript Factory Functions vs Constructor Functions vs Classes