realLifeinfo

How to define an arbitrary function in JavaScript

Nerd, Solutions 

JavaScript is one of the popular programming language in computer world. JavaScript popularity comes from the fact that is the main scripting language for web that runs in the browser.

Sponsored

Many functionalities and behaviors of websites are programmed using JavaScript.

JavaScript as the programming languages contains many features, but as in this article we are going to talk on how to create an arbitrary function in JavaScript. In a brief definition, an arbitrary function is the function that allow an unknown number of arguments to be used. Many programming languages like C, C++ and Python support this feature.

Defining arbitrary function in JavaScript

Defining arbitrary function in JavaScript depends on, which JavaScript (ECMAScript) version you are using. The syntax is slightly difference from two widely used JavaScript versions. We are going to talk on how to define an arbitrary function in ECMAScript 5, and ECMAScript 6.

Define arbitrary function in ECMAScript 5

There is no special syntax in defining an arbitrary function in ECMAScript 5. We use normal function definition as other function in JavaScript. The only catch is we access the hidden variable that is available in all function definitions in JavaScript. The arguments variable is used to store all variables that are passed to the function during function invocation.

Example:

Sponsored

function summation(a,b) {

      var len = arguments.length - 1; // get the arguments length

      var total = 0;

      while (len >= 0) {

          total += arguments[len--1]

     }

return total;

}

As you can see from our example function, we access the arguments variable that is array like object, and loop through it for all arguments that may be passed to it during function invocation.

Example of arbitrary function invocation in ECMAScript 5:

summation(10,10,10); // this will return 30 as result

summation(10,10); this will return 20 as result

This function can be used with any number of arguments.

Define arbitrary function in ECMAScript 6

In a newer standard version of JavaScript (ECMAScript 6) it's very easy to define an arbitrary function. This syntax closely resembles definition of arbitrary function in Python. In ECMAScript 6 we required to define our own argument that will be used to store all unspecified passed argument during the function invocation.

One advantage of this method is we can access specified argument separately from unspecified ones, which is not possible in ECMAScript 5 syntax. The name of this syntax is known as rest operator.

Example of arbitrary function definition in ECMAScript 6:

Sponsored

function summation(a,b, ...args){

     let total = 0;

     let len = args.length - 1;

     let result = a + b; // work with specified argument independently

     while ( len >= 0) {

           total += args[len--]

     }

     total += result;

    return total;

}

In this example we can work with specified argument independently from unspecified ones. You can see we find the result of two specified arguments, and then add the result to that of unspecified arguments.

Example of arbitrary function invocation in ECMAScript 6:

summation (10,10,10); //the result will be 30

summation (10,10); // the result will be 20

This function will work with any number of arguments. 

Read how to define an arbitrary function Python programming language.

Sponsored