Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 21, 2019 02:05 pm GMT

JavaScript 101: Arrow Functions

Arrow functions - they're the more concise version of regular functions and they've been gaining popularity since they were first introduced in ES6. Not only is the syntax much cleaner but they also provide implicit returns which we'll dive into.

Hold up! If you haven't read the first part in this series, JavaScript 101: Breaking Down Functions, then make sure to check that out.

Let's start with how the arrow function's syntax is different from regular functions.

Here we have a regular function:

helloWorld = function helloWorld(name) { console.log('Hello ' + name);}

If we wanted to use an arrow function, it would look like this:

const helloWorld = name => {  console.log('Hello ' + name);}

There are some key differences with arrow functions. We've dropped having to declare the function using the function keyword. Our parameters are a bit different than before as well. They now come after an equal sign and before the fat arrow (=>).

You may have also noticed that we're now declaring our arrow function as a variable. That's because arrow functions are anonymous functions or functions that are declared without a name. You don't have to assign them to a variable but doing so allows you trace them more easily when you have an error.

Quick tip: If you only have one parameter the parentheses are optional. If you don't have any parameters then you'll need to use empty parentheses.

Here's our example with multiple parameters:

const helloWorld = (name, emoji) => {  console.log(emoji + ' Hello ' + name);}

And our example with no parameters:

const helloWorld = () => {  console.log('Hello');}

Now that we've covered the syntax of arrow functions let's talk about another big benefit - implicit returns! This will make your code even shorter than before. Let's take the example we've been using and switch to a return instead of a console.log.

const helloWorld = name => {  return 'Hello ' + name;}

Because we're only returning a single line of code we can use the arrow function's ability to do an implicit return and rewrite our function like so:

const helloWorld = name => 'Hello ' + name;

When utilizing implicit returns, you're able to drop the return keyword as well as the curly brackets. This makes for really nice one-line functions.

Implicit returns aren't the only big difference between arrow functions and regular functions. Another big one is how they handle the this keyword.

In regular functions, the this keyword is bound depending on the context in which it was called. However, inside of arrow functions, this is lexically bound meaning that it's static and determined by the scope that it's in.

This is still something that I'm trying to grasp myself but JavaScript Kit has a great explanation if you're wanting to dive in more.

Be sure to follow me on Twitter and Instagram for lots of posts about tech, and if I'm being honest, lots of posts about dogs too.


Original Link: https://dev.to/karaluton/javascript-101-arrow-functions-jje

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To