Syntax, Parameter and Return Value
Syntax:
_.partial(func, [partials])
Parameters:
-
func (Function): The function to partially apply arguments to.
- [partials]: The arguments to be partially applied.
Return Value:
(Function) - Returns the new partially applied function.
Examples
Creating a Specific Logger Function:
JavaScript
var _ = require('lodash');
function logger(level, message) {
console.log(`[${level}] - ${message}`);
}
var infoLogger = _.partial(logger, 'INFO');
infoLogger('System is up and running');

You can also try this code with Online Javascript Compiler
Run Code
Output:
'[INFO] - System is up and running'
Demonstrates creating a specific logger function for the 'INFO' level.
Pre-filling Arguments for Event Handlers:
JavaScript
function handleEvent(eventName, event) {
console.log(`Event ${eventName} occurred:`, event.type);
}
var handleMouseClick = _.partial(handleEvent, 'Mouse Click');
document.addEventListener('click', handleMouseClick);

You can also try this code with Online Javascript Compiler
Run Code
// On click, logs: 'Event Mouse Click occurred: click'
Shows using _.partial() to create a specific event handler for mouse clicks.
Setting Default Parameters for API Calls:
JavaScript
function fetchData(url, queryParams) {
console.log(`Fetching data from ${url} with params`, queryParams);
// API call logic
}
var fetchUser = _.partial(fetchData, '/api/users', { active: true });
fetchUser();

You can also try this code with Online Javascript Compiler
Run Code
// Fetching data from /api/users with params { active: true }
An example of using _.partial() to set default parameters for an API call function.
Creating a Bind Function for Specific Element:
JavaScript
function bindToElement(element, data) {
element.textContent = JSON.stringify(data);
}
var bindToMyElement = _.partial(bindToElement, document.getElementById('myElement'));
bindToMyElement({ key: 'value' });

You can also try this code with Online Javascript Compiler
Run Code
// Binds { key: 'value' } to the 'myElement' element
Demonstrates creating a function to bind data to a specific DOM element.
Frequently Asked Questions
Can _.partial() handle functions with multiple arguments?
Yes, _.partial() can be used with functions that take multiple arguments. It allows you to pre-fill any number of these arguments as needed.
How does _.partial() differ from _.bind()?
While both _.partial() and _.bind() can pre-fill arguments, _.bind() also sets the this context of the function, whereas _.partial() only deals with arguments.
Is it possible to use _.partial() with asynchronous functions?
Yes, _.partial() can be used with asynchronous functions, including functions that return promises. It works by pre-filling arguments, regardless of the function's synchronous or asynchronous nature.
Conclusion
Lodash's _.partial() method is a powerful tool for creating more specific functions from general ones by pre-filling some of their arguments. It enhances the flexibility and modularity of function calls, allowing for more concise and targeted function execution.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.