Currying in JavaScript

Currying is a concept in functional programming where a function that takes multiple arguments is transformed into a series of functions, each taking only one argument at a time. The curried function allows you to partially apply arguments and returns a new function until all the arguments are provided and the final result is returned.

In JavaScript, currying can be achieved using closures and function composition. Here's an example to illustrate how currying works:

function logger(level) {
  return function(message) {
    console.log(`[${level}]: ${message}`);

// Creating different loggers for different levels
const infoLogger = logger('INFO');
const errorLogger = logger('ERROR');
const debugLogger = logger('DEBUG');

// Using the loggers
infoLogger('This is an informational message.');
errorLogger('An error occurred.');
debugLogger('Debugging information.');

In the example above, we have a logger function that takes a level parameter and returns a new function that takes a message parameter. The returned function logs the message along with the specified log level.

By currying the logger function, we can create multiple specialized loggers by fixing the level argument. In this case, we create infoLogger, errorLogger, and debugLogger with different log levels.

When we invoke the loggers with the message argument, they will log the message with the appropriate log level. This allows us to have more control over the logging output and easily switch between different levels of logging based on our needs.

Currying in this example provides a convenient way to create reusable logging functions with preconfigured behavior. We can easily create loggers with different log levels without having to repeat the log level parameter every time we want to log a message.