Append Function Arguments

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore how to use the partialRight() function in JavaScript to create new functions with pre-specified arguments. This powerful function allows us to easily append arguments to existing functions, enabling us to create new functions with specific behaviors without having to redefine the original function. We'll see examples of how to use partialRight() to streamline our code and make it more modular.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/AdvancedConceptsGroup(["`Advanced Concepts`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/str_manip("`String Manipulation`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28134{{"`Append Function Arguments`"}} javascript/data_types -.-> lab-28134{{"`Append Function Arguments`"}} javascript/arith_ops -.-> lab-28134{{"`Append Function Arguments`"}} javascript/comp_ops -.-> lab-28134{{"`Append Function Arguments`"}} javascript/str_manip -.-> lab-28134{{"`Append Function Arguments`"}} javascript/spread_rest -.-> lab-28134{{"`Append Function Arguments`"}} end

Function that Appends Arguments

To create a function that appends arguments to the ones it receives, follow these steps:

  1. Open the Terminal/SSH and type node to start coding practice.
  2. Use the spread operator (...) to append partials to the list of arguments of fn.
  3. Use the following code to create the function:
const partialRight =
  (fn, ...partials) =>
  (...args) =>
    fn(...args, ...partials);
  1. Test the function with an example, such as:
const greet = (greeting, name) => greeting + " " + name + "!";
const greetJohn = partialRight(greet, "John");
greetJohn("Hello"); // 'Hello John!'

Summary

Congratulations! You have completed the Append Function Arguments lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like