Unary 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 "Unary Function Arity" in JavaScript. We will learn how to create a function that accepts only one argument and ignores any additional arguments. This lab will also demonstrate how to call the provided function with just the first argument supplied. By the end of this lab, you will be able to implement this technique in your code to improve its efficiency and readability.


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-28674{{"`Unary Function Arity`"}} javascript/data_types -.-> lab-28674{{"`Unary Function Arity`"}} javascript/arith_ops -.-> lab-28674{{"`Unary Function Arity`"}} javascript/comp_ops -.-> lab-28674{{"`Unary Function Arity`"}} javascript/higher_funcs -.-> lab-28674{{"`Unary Function Arity`"}} end

Understanding Unary Function Arity

To start coding, open the Terminal/SSH and type node.

Unary function arity refers to a function that takes only one argument, ignoring any additional arguments.

The provided function fn can be called with just the first argument supplied. To create a unary function, use the following code:

const unary = (fn) => (val) => fn(val);

An example of using unary with parseInt function is shown below:

["6", "8", "10"].map(unary(parseInt)); // [6, 8, 10]

Summary

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

Other JavaScript Tutorials you may like