Learnitweb

What is a unary function?

A unary function is a function that accepts exactly one argument. The term “unary” comes from the prefix “uni-“, meaning “one.” Unary functions are commonly used in mathematics and programming.

Characteristics of a Unary Function

  • Single Parameter: A unary function takes one input value.
  • Simple and Focused: Unary functions are often small and perform a single operation.
  • Common in Functional Programming: Many higher-order functions, such as Array.map, Array.filter, and Array.reduce, use unary functions as callbacks.

Example of Unary Function

const negate = (num) => -num;
console.log(negate(5)); // Output: -5