What are factory functions in JavaScript ? Last Updated : 09 Jan, 2024 Comments Improve Suggest changes 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 ? 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 Using the function* Declaration in JavaScript The function* declaration is used to define a generator that returns a Generator object. Generators are very powerful for asynchronous programming as they aim to resolve callback problems. In a generator, the yield keyword is used instead of return. The yield statement suspends the functionâs execut 2 min read JavaScript Function() Constructor The JavaScript Function() constructor is used to create new function objects dynamically. By using the Function() constructor with the new operator, developers can define functions on the fly, passing the function body as a string. This allows for greater flexibility in situations where functions ne 2 min read Abstraction in JavaScript In JavaScript, Abstraction can be defined as the concept of hiding the inner complex workings of an object and exposing only the essential features to the user. Hiding Complexity: Implementation is hidden, it shows only the necessary details.Modularity: Code is organized in a reusable form, which im 4 min read All about Functions and Scopes in JavaScript In this article, we will cover all the basic concepts of JS functions, callbacks, scopes, closures in-depth which would help you to - understand different types of function declarations.make better use of functions.understand how different scopes and scope chain works in JS.learn about closures and 10 min read What is a typical use case for anonymous functions in JavaScript ? In this article, we will try to understand what exactly an Anonymous function is, and how we could declare it using the syntax provided in JavaScript further we will see some examples (use-cases) where we can use anonymous functions to get our results in the console. Before proceeding with the examp 4 min read Like