Binary Function Arity

JavaScriptJavaScriptBeginner
Practice Now

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

Introduction

In this lab, we will explore the concept of Binary Function Arity in JavaScript. We will learn how to create a function that accepts up to two arguments and ignores any additional arguments. Through practical examples, we will see how this technique can be used to simplify our code and make it more efficient.


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`") subgraph Lab Skills javascript/variables -.-> lab-28172{{"`Binary Function Arity`"}} javascript/data_types -.-> lab-28172{{"`Binary Function Arity`"}} javascript/arith_ops -.-> lab-28172{{"`Binary Function Arity`"}} javascript/comp_ops -.-> lab-28172{{"`Binary Function Arity`"}} javascript/higher_funcs -.-> lab-28172{{"`Binary Function Arity`"}} end

Function that accepts up to two arguments

To begin coding, open the Terminal/SSH and enter node.

The binary function is created with the ability to accept up to two arguments while disregarding any additional ones.

The provided fn function is called with the first two arguments given.

Here is the code:

const binary = (fn) => (a, b) => fn(a, b);

And here's an example usage:

["2", "1", "0"].map(binary(Math.max)); // [2, 1, 2]

Summary

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

Other JavaScript Tutorials you may like