Monday, November 18, 2024
Google search engine
HomeLanguagesJavascriptHow to define a function in ES6 ?

How to define a function in ES6 ?

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("Geeksforneveropen");
let display = name = console.log(name);
display("Geeksforneveropen");

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments