What are factory functions in JavaScript ? Last Updated : 09 Jan, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report In JavaScript, a factory function is a function that returns an object. It is a way of creating and returning objects in a more controlled and customizable manner. Factory functions are a form of design pattern that enables the creation of objects with specific properties and behaviors. 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 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. JavaScript // Function creating new objects // without use of 'new' keyword function createRobot(name) { return { name: name, talk: function () { console.log('My name is ' + name + ', the robot.'); } }; } //Create a robot with name Chitti const robo1 = createRobot('Chitti'); robo1.talk(); // Create a robot with name Chitti 2.O Upgraded const robo2 = createRobot('Chitti 2.O Upgraded'); robo2.talk(); OutputMy name is Chitti, the robot. My name is Chitti 2.O Upgraded, the robot.Example 2: In this example, the Person factory function efficiently creates person objects by encapsulating the process of setting name and age properties. Each person object includes a greeting method for generating a customized message. Instances, such as person1 and person2, are easily created using the factory function, showcasing a concise and reusable approach to object creation in JavaScript. JavaScript // Factory Function creating person let Person = function (name, age) { // creating person object let person = {}; // parameters as keys to this object person.name = name; person.age = age; // function to greet person.greeting = function () { return ( 'Hello I am ' + person.name + '. I am ' + person.age + ' years old. ' ); }; return person; }; let person1 = Person('Abhishek', 20); console.log(person1.greeting()); let person2 = Person('Raj', 25); console.log(person2.greeting()); OutputHello I am Abhishek. I am 20 years old. Hello I am Raj. I am 25 years old. Comment More infoAdvertise with us Next Article What are factory functions in JavaScript ? A abhishekcs001 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads What is the factory function in Angular ? In Angular, the factory function is always inclined towards class and constructors. Generally, the factory function is used to provide service as a dependency in any angular application. A factory function generates an object, provides it with some logic, executes the function, and returns the objec 4 min read What is a Constructor in JavaScript? A constructor in JavaScript is a special function that is used to create and initialize objects. When we want to create multiple objects with similar properties and methods, the constructor is used as a blueprint to create similar objects. This is useful when you want to create multiple objects with 8 min read What is the (function() { } )() construct in JavaScript? If you've ever played around with JavaScript, you might have seen this expression. It's like a strange set of symbols, but it has a special name that is an immediately invoked function expression, or IIFE. In this article, we will understand each element of the expression as well as the functionalit 3 min read What is the first class function in JavaScript ? First-Class FunctionA programming language is said to have First-class functions if functions in that language are treated like other variables. So the functions can be assigned to any other variable or passed as an argument or can be returned by another function. JavaScript treats function as a fir 2 min read Interesting Facts about JavaScript Functions Let us talk about some interesting facts about JavaScript Functions that can make you an efficient programmer.Functions Are First-Class CitizensJavaScript treats functions as first-class citizens, meaning they can be:Stored in variablesPassed as arguments to other functionsReturned from other functi 3 min read Like