Understanding Unary Functions in JavaScript

Understanding Unary Functions in JavaScript

JavaScript is a popular programming language used for creating dynamic web content. When it comes to JavaScript interview questions, one question that often comes up is, “What is a unary function?” In this article, we will explore this concept in detail, its usage in JavaScript with code examples, and related concepts or methods that may help to clarify the topic.

What is a Unary Function?

In mathematics, a unary function is a function that takes only one argument. Similarly, in computer science, a unary function is a function that takes only one parameter. In JavaScript, a unary function is a function that takes one argument.

Unary functions are commonly used in programming to transform values. They take in a single input value and return a transformed value. Some common examples of unary functions include the parseInt() function, which converts a string to an integer, and the parseFloat() function, which converts a string to a floating-point number.

Usage of Unary Functions in JavaScript

Unary functions are used in JavaScript to transform values in various ways. Let’s take a look at some examples:

// Example 1
const double = x => x * 2;
console.log(double(4)); // Output: 8

// Example 2
const square = x => x * x;
console.log(square(5)); // Output: 25

// Example 3
const toLowerCase = str => str.toLowerCase();
console.log(toLowerCase("HELLO")); // Output: "hello"

In the first example, we define a double function that takes in a number and returns the double of that number. In the second example, we define a square function that takes in a number and returns the square of that number. In the third example, we define a toLowerCase function that takes in a string and returns the lowercase version of that string.

There are several related concepts or methods that can help to clarify the topic of unary functions in JavaScript. Let’s take a look at some of them:

Currying

Currying is a technique used in functional programming where a function with multiple arguments is transformed into a sequence of functions that each take a single argument. This can be useful when working with unary functions since it allows you to create new functions by partially applying the original function.

// Example using currying
const add = x => y => x + y;
const add2 = add(2);
console.log(add2(3)); // Output: 5

In the above example, we define an add function that takes two arguments and returns their sum. We then use currying to create a new function add2 that adds 2 to any number passed to it.

Higher-order Functions

Higher-order functions are functions that take one or more functions as arguments or return a function as their result. They are commonly used in functional programming to create more complex functions from simpler ones.

// Example using higher-order functions
const map = (arr, fn) => arr.map(fn);
const double = x => x * 2;
console.log(map([1, 2, 3], double)); // Output: [2, 4, 6]

In the above example, we define a map function that takes an array and a function as arguments and returns a new array with the function applied to each element of the original array. We then define a double function and use it with the map function to create a new array with each element doubled.

Conclusion

In conclusion, a unary function is a function that takes one argument in JavaScript. Unary functions are commonly used in programming to transform values. They can be combined with other concepts like currying and higher-order functions to create more complex functions from simpler ones. Understanding unary functions is important for any JavaScript developer, and it is a concept that is likely to come up in interviews.