JavaScript Arrow Functions

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
Search for a command to run...

I'm a full stack developer based in Australia. I write articles about Node.js, React, JavaScript & Web Development to make your learning journey easier ๐
No comments yet. Be the first to comment.
Introduction Website performance is an important aspect of web development. It consists of mainly two things: The speed of your website How usable is your website for visitors? Importance of website performance Website performance is very importa...

There are plenty of native web APIs that have a variety of uses. Here are 5 useful APIs that I use quite often in my web development projects. Clipboard API The clipboard API is a simple method to copy text to a user's clipboard. This can be used in ...

Hey All ๐ In this article, we'll be covering the Fetch API in JavaScript how you can use it to make HTTP requests. What is the Fetch API? The fetch() method provides a simple way to make HTTP requests in JavaScript. It is natively in the browser, me...

Hey All ๐ In this article, we'll be covering 4 mistakes commonly made by people while learning to code that will seriously impact their coding journey. #1 - Copying and Pasting The first mistake I'll talk about is copying and pasting. We've all don...

Introduction There are plenty of units in CSS. From px to vh, there is an abundance of units all with their distinctive use cases. Considering the amount of options, its perfectly reasonably to be confused as to which unit you should use and where. T...

In this article, we'll be covering arrow functions and how to implement them in JavaScript. Lets start off by discussing what are arrow functions and how they can be implement in JavaScript.
Arrow functions allow us to define functions with shorter syntax. Arrow functions can be used in replacement of a traditional function function() {}.
Lets write a function that takes in 2 numbers as parameters a and b, then returns the sum.
As a regular function:
function sum(a, b) {
return a + b
}
Now, as an arrow function:
const sum = (a, b) => a + b
You can clearly see how much shorter the syntax is.
When using an arrow function, parameters go at the start in a pair of parenthesis. These are then followed by a => which declares an arrow function. After that you would enter {} and whatever code you wish to run. However when returning a value, you can exclude the curly braces.
Additionally, if there is only one parameter, you can exclude the parenthesis around it.
This would look like this:
const addOne = num => num + 1
If there are no parameters for your function you would simply use an empty pair of parenthesis followed by the usual. Like this:
const sayHello = () => {
console.log("Hello")
}
Arrow functions can be used in replacement of any function use case. For instance, in React, you could use an arrow function to define a functional component rather than a regular function.
Another use case may be as a callback function. For instance, if we have an array of users that looks like this:
const users = [
{
name: "John",
age: 24
},
{
name: "Jane",
age: 32
},
{
name: "Blake",
age: 12
}
]
And we want to use the JavaScript .filter method to select users who are over 18:
const adults = users.filter(user => user.age > 18)
This is one use case where arrow functions can significantly reduce the amount of code you have to write. This also applies to other array methods like .map for instance.
this KeywordIn standard functions, the this keyword represents the object that called the function, which may be a button or even the document. However, in an arrow function, the this keyword represents the object that defined the arrow function, not the element/object that called the function.
I hope you've learned how to begin using arrow functions in JavaScript. ES6 brought a lot of new and improved features to JavaScript, such as arrow functions and classes.
๐ Thanks for reading this article!
If you like what I do and would love to see more related content, follow me on my other social platforms:
GitHub: Blake-K-Yeboah
LinkedIn: Blake-K-Yeboah
You can also show your support by buying me a coffee ๐