Unary function in javascript

A unary function is a mathematical function that takes only one argument or input. It is a function of a single variable, which means that it operates on a single operand. The term “unary” comes from the Latin word “unarius” meaning “consisting of one.”

For example, the function f(x) = x^2 is a unary function because it takes only one input, x, and returns the square of x. Another example of a unary function is the absolute value function f(x) = |x|, which returns the positive value of x regardless of its sign.

Unary functions are often used in computer programming, where they are used to transform or manipulate data. They can also be used in various fields of mathematics, including calculus, linear algebra, and logic.

Here’s an example of a unary function in mathematics:

f(x) = 2x

This function takes a single input x and returns the result of multiplying x by 2. For example, if we plug in x = 3, we get:

f(3) = 2(3) = 6

So, f(3) returns the value 6. This function is an example of a unary function because it has only one input variable (x) and operates on that single variable.

 

// Define a unary function that doubles its input

function double(x) {

  console.log( x * 2);

}

// Use the function to double a value

const num = 3;

const result = double(num); // result is now 6

Leave a Reply