Skip to content
the poor coder

arrow expression javascript

Poor Man
Published
Reading time
1 min read
On this page

Arrow Expression in JavaScript#

Arrow functions, also known as arrow expressions, were introduced in ECMAScript 6 (ES6) as a new syntax for defining functions in JavaScript. They provide a more concise way of writing functions and have a shorter syntax than traditional functions.

Syntax#

The basic syntax of an arrow function is:

Text
() => { // function body }

or

Text
parameter => { // function body }

For example, let's say we have a traditional function that adds two numbers:

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

The same function can be written using an arrow function as:

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

If the function body consists of only one statement, the curly braces and the return keyword can be omitted:

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

Advantages#

  • Shorter syntax: Arrow functions have a more concise syntax than traditional functions, making the code easier to read and write.
  • No binding of this keyword: Arrow functions do not bind their own this keyword, which means they can use the this value of the enclosing lexical scope.
  • Implicit return: If the function body consists of only one statement, the return keyword and the curly braces can be omitted.

Disadvantages#

  • No arguments object: Arrow functions do not have their own arguments object, which means they cannot be used with the arguments keyword to access the function arguments.
  • No named function expressions: Arrow functions cannot be used as named function expressions, which means they cannot be used with the Function constructor or as a method of an object.
  • No prototype property: Arrow functions do not have a prototype property, which means they cannot be used as constructors to create new objects.

Overall, arrow functions are a useful addition to JavaScript, providing a more concise way of writing functions and eliminating the need for the this keyword. However, they also have some limitations that should be considered before using them in all situations.

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