Limiting Function Arguments in JavaScript

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore the Function Arity concept in JavaScript. The purpose of this lab is to create a higher-order function that accepts a function and a number n as arguments and returns a new function that only accepts up to n arguments, ignoring any additional ones. This will help us understand how to limit the number of arguments passed to a function in JavaScript.


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/AdvancedConceptsGroup -.-> javascript/higher_funcs("`Higher-Order Functions`") javascript/AdvancedConceptsGroup -.-> javascript/spread_rest("`Spread and Rest Operators`") subgraph Lab Skills javascript/variables -.-> lab-28322{{"`Limiting Function Arguments in JavaScript`"}} javascript/data_types -.-> lab-28322{{"`Limiting Function Arguments in JavaScript`"}} javascript/arith_ops -.-> lab-28322{{"`Limiting Function Arguments in JavaScript`"}} javascript/comp_ops -.-> lab-28322{{"`Limiting Function Arguments in JavaScript`"}} javascript/higher_funcs -.-> lab-28322{{"`Limiting Function Arguments in JavaScript`"}} javascript/spread_rest -.-> lab-28322{{"`Limiting Function Arguments in JavaScript`"}} end

How to Create a Function with a Specific Number of Arguments

To create a function that accepts a specific number of arguments and ignores any additional arguments, follow these steps:

  1. Open the Terminal/SSH and type node to start practicing coding.

  2. Use the following code to create your function:

const ary =
  (fn, n) =>
  (...args) =>
    fn(...args.slice(0, n));
  1. Call the function you just created, ary, with two arguments: the function you want to limit the arguments for (fn) and the number of arguments you want to limit it to (n).

  2. Now you can use the new function to limit the number of arguments for any function you want. To do this, call your new function with the spread operator (...) and the arguments you want to limit.

Here's an example of how to use your new function:

const firstTwoMax = ary(Math.max, 2);
[[2, 6, "a"], [6, 4, 8], [10]].map((x) => firstTwoMax(...x)); // [6, 6, 10]

In this example, firstTwoMax is a new function that limits the Math.max function to only accept the first two arguments. The map method is used to apply the new function to each array in the outer array, returning the maximum of the first two elements of each inner array.

Summary

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

Other JavaScript Tutorials you may like