Open In App

How to define a function in ES6 ?

Last Updated : 12 Jan, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will try to understand basic details which are associated with the function definition, like syntax declaration of a function or some examples associated with different types of functions declarations in ES6 (EcmaScript-6). 

Let us first understand what exactly the function is and how to declare a function in ES6 using different syntaxes.

function display(name) {
  console.log(name)
}

display("Geeksforgeeks");
let display = name = console.log(name);
display("Geeksforgeeks");

Functions:

  • Functions are one of the basic fundamental blocks in JavaScript.
  • It contains those lines of codes that the user wishes to either reuse or just invoke only when that function is invoked.
  • A function may take no input parameter or may have a different number of parameters.
  • A function must have either a return statement or a print statement which would be executed while that function is invoked at the point during the program execution.
  • A function can be declared with different syntaxes, few of them are illustrated in the above pictorial representation.

Following are some of the syntax which can be used to declare a function in ES6:

Syntax 1: The first syntax is the basic syntax that is valid from the starting ES version till this ES6 version as well.

function function_name (list_of_parameters) {
    ...
}

If one wishes to store the function in a variable then by using the following syntax one can easily do that.

let variable = function (list_of_parameters) {
    ...
}

Syntax 2: Now another syntax of declaring a function is the Arrow function syntax which is described as follows and the calling of these particular type of function is quite simple as that of the previous type of functions which is just writing the name followed by the round braces, including the parameters if any (like this display(name) and so on).

let variable = (list_of_parameters) => {
    ...
}

Even if we don’t want to write the round braces then also we may proceed by using the following syntax.

let variable = parameters => {
    ...
}

Now that we have analyzed who writes a function in ES6 let us quote some examples which would help us to understand the functions declarations in a better and effective way.

Example 1: In this example, we will see the normal function declaration.

Javascript




<script>
    function squareOfNumber(number) {
        return number * number;
    }
  
    console.log(squareOfNumber(5));
    console.log(squareOfNumber(14));
    console.log(squareOfNumber(19));
</script>


Output: The output of the above code snippet is shown below-

25
196
361

Example 2: In this example, we will be using Rest parameters inside the normal function (which actually means that we are passing an infinite number of values as parameters of a function) and further checking the length of passed parameters.

Javascript




<script>
    function checkParameterLength(...parameters) {
        return parameters.length;
    }
  
    console.log(checkParameterLength(2, 5, 8));
    console.log(checkParameterLength(10));
    console.log(checkParameterLength(50, 8, 6, 8, 10));
    console.log(checkParameterLength(52, 10));
    console.log(checkParameterLength());
</script>


Output: The output of the above code snippet would be as follows-

3
1
5
2
0

Example 3: In this, we will using arrow function syntax, and by using that we will be finding out the factorial of the given number.

Javascript




<script>
    let factorial = (number) => {
        if (number <= 1) return 1;
        else return number * factorial(number - 1);
    }
  
    console.log(factorial(10));
    console.log(factorial(19));
    console.log(factorial(5));
</script>


Output: The output of the above code snippet would be as follows-

3628800
121645100408832000
120


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads