- Overview
- Transcript
6.4 Arrow Functions
Arrow functions give us a much more concise syntax for anonymous functions. this
works a little differently in an arrow function: it is lexically bound. Let’s take a closer look at how to use arrow functions and how this
works with them in this lesson.
Syntax
=>
1.Introduction2 lessons, 07:42
1.1Introduction02:12
1.2Setup05:30
2.Language Fundamentals8 lessons, 1:00:53
2.1Variables06:33
2.2Data Types11:28
2.3Arithmetic, Assignment, and Comparison Operators10:24
2.4Unary, Logical, Comma, and Spread Operators09:02
2.5Operator Precedence03:50
2.6Reserved Words04:17
2.7Strict Mode04:34
2.8Functions10:45
3.Data Structures5 lessons, 22:52
3.1Arrays04:29
3.2Objects04:30
3.3Sets04:57
3.4Maps04:21
3.5Weak Maps and Weak Sets04:35
4.Controlling Program Execution7 lessons, 37:06
4.1Conditionals07:49
4.2Switch Statements04:41
4.3The For Loop06:39
4.4The `for .. in` Loop05:17
4.5The `for .. of` Loop04:02
4.6Iterators05:03
4.7While Loops03:35
5.Using JavaScript13 lessons, 1:44:36
5.1Working With Strings09:32
5.2Template Literals05:46
5.3Working With Numbers06:57
5.4Working With Arrays12:53
5.5Iterating and Transforming Arrays07:33
5.6Working With the Object Type13:55
5.7Object Literal Extensions06:45
5.8Working With Object Instances06:45
5.9Getters and Setters05:00
5.10Custom Objects11:28
5.11The `Math` API04:54
5.12Working With Dates and Times08:10
5.13The `Array` Constructor04:58
6.Functions8 lessons, 56:07
6.1The `this` Object06:15
6.2Working With Functions10:11
6.3Scope07:37
6.4Arrow Functions06:59
6.5Generator Functions08:13
6.6Closures05:00
6.7Prototypes06:26
6.8Default and Rest Parameters05:26
7.Miscellaneous6 lessons, 52:39
7.1Destructuring Assignments08:09
7.2AJAX08:30
7.3Regular Expressions10:51
7.4More About Regular Expressions08:38
7.5Classes06:48
7.6ES Modules09:43
8.Working With the DOM6 lessons, 37:39
8.1Selecting HTML Elements05:02
8.2Manipulating HTML Elements07:40
8.3DOM Traversal05:25
8.4Adding and Removing Elements04:45
8.5Creating Elements and Other Nodes04:39
8.6DOM Events10:08
9.Web APIs4 lessons, 17:41
9.1The Selector API03:03
9.2Geolocation05:29
9.3Web Storage05:24
9.4Web Workers03:45
10.Asynchronous JavaScript5 lessons, 26:23
10.1Promises09:52
10.2Promise Chaining05:11
10.3The async Keyword03:21
10.4The await Keyword04:04
10.5More About async and await03:55
11.Conclusion1 lesson, 00:43
11.1Conclusion00:43
6.4 Arrow Functions
Hi folks, in this lesson, we're going to look at an alternative syntax that we can use to define functions, arrow functions, or fat arrow functions as they're also known. Arrow functions are mostly a syntactic sugar for using a shorter syntax which is equivalent to a function expression. But they do also make use of a lexical this, which is very different from regular functions. Let's take a quick look. So a typical arrow function will look something like this. So let's just look at the output in our browser, and then we'll come back and talk about the syntax. So the square of 3 is 9, as you were probably expecting. Okay, so when we define an arrow function, we don't need to use the function keyword. The function keyword doesn't appear in this code at all, but as you can see, it's definitely a function because we are invoking it. What makes this an arrow function is the arrow function syntax. And really that’s this part of the code here, the equals and greater than sign. We also don’t provide a name for the function, so arrow functions are always anonymous functions. The body of the function in this case is contained within curly brackets, which is necessary because there are multiple expressions inside the function. Those expressions are where we create the square variable and where we return the square variable. So this should now work in the same way as before. And now actually because we're down to just the single expression inside our arrow function, we don't need to use the curly brackets. And now that we aren't using the curly brackets, we don't need to use the return statement. Let's just go back to the browser. And we can see that the function is still working in the same way that it did before, even though the syntax is now quite different than when we first started. So we've managed to condense our function down to the single line, and that really is the power and the beauty of arrow functions. Very often, they can be simple one-liners like this. So, this is now known as an implicit return because we don't explicitly use the return keyword. We only need to do that when we are using an arrow function that contains multiple expressions and thus requires the curly brackets. The parameter that gets passed into the function is specified before the fat arrow and in this case we're only using a single parameter. If we want to use multiple parameters however, we need to use a slightly different format once again. So because we're now passing multiple parameters into the arrow function, we need to surround these parameters with parentheses and separate them with a comma. This is the same even if the function didn't accept any parameters. So even though we aren't passing any parameters at all now, we need to use the parentheses. We can only get rid of the parentheses if we're passing a single argument into the arrow function. So we saw an example where we used curly brackets earlier. What happens if we want to return an object literal from an arrow function? So the function actually is returning undefined at this point. The reason why it's returning undefined is because, in this case, we want to return an object literal but when we use curly brackets, that tells JavaScript that we're using a multi-line function. Even though in this case it isn't actually a multi-line function, it still confuses the JavaScript engine. So when we're using an arrow function and we do want to return an object literal, we just need to wrap that object literal in parentheses. And now we see the object literal as we were expecting. So these are some of the variations in the formats of arrow functions. You might see any number or combination of those variations depending on how the arrow function is being used. So one other very important feature of arrow functions is that they have a lexical this. And what that means is tht they take their value of this from the surrounding code. Whatever is in the this context around the function is what will be the this context inside of the arrow function according to the regular rules of this. So in this function's case, that would be undefined because we are inside the function. That function is the outer containing iffy function. A consequence of the lexical this is that arrow functions can not change their value of this. We can use arrow functions with bind, call, or apply. But regardless of what we specify as the this object, when we're using those methods, inside an arrow function, this will always be the lexical this. A consequence of not being able to change the this value inside an arrow function is that we can't use arrow functions as constructors. Invoking an arrow function with a new keyword will throw an error. Another limitation is that arrow functions cannot be generator functions. We're gonna look at generator functions in the next lesson. So for now, just be aware that arrow functions cannot be generator functions. So in this lesson, we looked at arrow functions, which have two main benefits. The first benefit is a much more concise syntax than other functions. And the second benefit is the lexical this, which, in some situations, can be very useful. We saw that there are several different variations that the arrow function might take, depending on how we want to use the function. We saw that we can pass zero or more than one parameter using parentheses, but if we're just passing a single argument, we don't need to use parentheses. We also saw that we can use curly brackets if we want to use multiple expressions inside the arrow function. And that if we want to return an object literal, we need to wrap it in parentheses. In the next lesson, we can move on to look at generator functions. Thanks for watching.