The Factory Function is similar to constructor functions/class functions, but instead of using new to create an object, factory functions simply creates an object and returns it.
Why it is useful?
If we have complex logic, and we have to create multiple objects again and again that have the same logic, we can write the logic once in a function and use that function as a factory to create our objects. It’s exactly the same as a real-world factory producing products.
Example 1: We have a factory function that will produce new robots with a single logic. Using this we can produce as many objects/robots as we want.
My name is Chitti, the robot. My name is Chitti 2.0 Upgraded, the robot.
Hello I am Abhishek. I am 20 years old. Hello I am Raj. I am 25 years old.