Skip to content
the poor coder

ES6 syntax for function in javascript

Poor Man
Published
Reading time
2 min read
On this page

ES6 Syntax for Function in JavaScript#

JavaScript is a programming language that is widely used for building web applications. The latest version of JavaScript is ES6, which introduced many new features, including a new syntax for writing functions. In this article, we will explore the ES6 syntax for functions and how they differ from the traditional way of writing functions.

Traditional Function Syntax#

The traditional way of defining a function in JavaScript involves the use of the function keyword, followed by the name of the function, a set of parentheses, and a set of curly braces that contain the code to be executed when the function is called. Here's an example:

Text

function add(a, b) {
  return a + b;
}

In this example, we have defined a function called add, which takes two parameters (a and b) and returns their sum.

Arrow Function Syntax#

The ES6 syntax for writing functions is called arrow functions. Arrow functions are a shorter way of writing functions that make use of the => symbol to define the function. Here's an example:

Text

const add = (a, b) => {
  return a + b;
}

In this example, we have defined a function called add, which takes two parameters (a and b) and returns their sum. We have used the => symbol to define the function instead of the function keyword, and we have enclosed the function body in curly braces.

Arrow functions are often used when we need to pass a function as an argument to another function or when we need to define a function inside another function. Here's an example:

Text

const numbers = [1, 2, 3, 4, 5];

const double = numbers.map((number) => {
  return number * 2;
});

In this example, we have defined an array of numbers and then used the map method to create a new array that contains the double of each number in the original array. We have used an arrow function to define the function that is passed as an argument to the map method.

Shorter Arrow Function Syntax#

There is an even shorter syntax for arrow functions that can be used when the function body contains only a single expression. In this case, we can omit the curly braces and the return keyword. Here's an example:

Text

const add = (a, b) => a + b;

In this example, we have defined a function called add, which takes two parameters (a and b) and returns their sum. We have used the shorter arrow function syntax to define the function, which consists of a single expression that returns the sum of the two parameters.

Conclusion#

The ES6 syntax for functions in JavaScript provides a more concise and expressive way of writing functions. Arrow functions are especially useful when working with arrays and when defining functions inside other functions. By using the arrow function syntax, we can write more readable and maintainable code.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC