JavaScript ES6 Arrow Functions Tutorial

Web Dev Simplified · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

The video tutorial covers the basics of JavaScript ES6 arrow functions, including their syntax, usage, and scoping of the this keyword, with examples and code snippets to illustrate their application.

Full Transcript

hello everybody kyle here from webdev simplified in today's video I'm going to be going over at one of the most important and useful features that came with the es6 version of javascript and that is arrow functions i'm going to be comparing them and contrasting them with the normal function syntax that you all already know so without any further ado let's get started in this javascript file I have four examples of functions to help us understand the differences between the syntax for normal functions and arrow functions right now all these are normal functions and we're going to convert them to arrow functions so the different types of functions we have is a named function with multiple parameters in this case - we have the named function with one parameter a named function with no parameters and down here lastly we have an anonymous function so it's just a function with no name so to get started we're going to convert this sum function first to an arrow function because it is the easiest format and probably the most common you'll run into when converting an arrow function so I'm just going to copy this paste it down here so we can convert this and compare and contrast then when we're done the first thing we need to do to convert an arrow function is completely remove the function keyword because with an arrow function this function keyword is assumed and now we need to assign it this actual function here to a variable so we're just going to call that variable sum - in this case so we'll just say sum - as our variable name we're going to set that equal to what we have left over which is going to be the beginning of the syntax for our error function and the reason we have to do this assignment here to a variable is because normal functions already create a variable of the name sum in this case with the function keyword but since we no longer have the function keyword we now need to create our own variable to store this function and the only change that we need to make to convert this function syntax from here to an arrow function is to add in the arrow that denotes that these are our parameters on the left side here inside of the parentheses and then we have an arrow to denote that this is a function and then inside of the brackets we put the actual function syntax that we're going to be using and now our sum - and some are both the exact same function just one uses the arrow syntax and the other uses a normal function tax we can go even further to reduce this code slightly more since as you can see we just have one line of code and it returns something this arrow functions can be changed so that we can remove this return completely and we can remove these curly braces and put everything on one line so if you do this everything after the arrow is assumed to be returned so this a plus B is going to be returned because there's no brackets and there's no return statement so it just assumes that whatever comes after the arrow here is going to be returned from the function and this again is exactly the same as our normal function that we have defined at the top here but as you can see this is much more concise and easier to use so let's do the same thing tor is positive function we'll copy this paste it down and as you know we can get rid of this function keyword change is positive to a variable we'll call it is positive to and we just add in the arrow and there we go again we have an arrow function but we can reduce this further by getting rid of those curly braces and there we go we have that exact same arrow function is positive to is now exactly the same as is positive but when we only have one single parameter we can actually completely remove these parentheses around the parameter and just have the single name of the parameter number here and that will still work and you'll see this very often because there's tons of use cases for these single parameter functions to be arrow functions so it'll be very common for you to see syntax like this and all it is saying is this is the argument of your function this number and then the arrow says we're returning everything after it since there's no curly braces afterwards inside of a function argument and then lastly we have this random number function which we paste this down we can do the exact same thing we can remove function replace it with a variable assignment and then we put in our arrow and again this is the correct syntax for a function with zero arguments what we want to do is we want to put these brackets right here are these parentheses and inside of it we just put nothing to denote that this has no functions that go into it and again if we want to do that fancy thing with the return we could just remove the return and the curly braces put everything on one line so it's a lot cleaner and easier to read and you may be thinking that this really doesn't save you very much because you still have to say let and define variable and it seems kind of clunky and I have to agree arrow functions aren't that useful when it comes to narrowing down the amount of words that you have to write any amount of code you have to write when it creating functions like this but where the arrow functions really shine is when you're creating anonymous functions or functions that have no name such as in this case when we're passing a function to another function so this is the normal function syntax that we would use and if we want to change this to an arrow function we remove the keyword of function and then we add in our arrow and there we go now we have our arrow function and we don't actually have to sign it to a variable by saying but and a variable naming equals we just have this function now here that we can define without having to use the function keyword and again we can put this onto one line instead of having to do it on multiple lines so we can make it even more concise by putting it on one line and removing it curly braces and now this line right here 23 is exactly the same as this entire code block up here from lines 19 to 21 it's just a more concise and easier way of using arrow functions but that's really not what makes arrow functions so great it's nice that they have a more concise syntax but what really makes arrow functions important is the fact that they redefine the this keyword inside of them as opposed to normal functions they use this keyword much differently so let me show you an example of how that will work on the left of my screen I have a simple person class which all it does is it takes in a name in the constructor and it sets this name to that name and then it has two functions one is called print name arrow and this function uses an arrow function inside of a timeout to print out the name of the person after the word arrow after a 100 millisecond delay and then those print name function down here which is exactly the same as the print name arrow function except for it uses the standard function syntax instead of the arrow function syntax so we can look at how this dot name is going to be different inside of the normal function versus the arrow function so before I start explaining the differences let's just run this code to show the differences so we can create a person object here we're gonna set it to new person and we just need to give it a name in our case we'll just give it the name of Bob and then we want to call those two function so we'll say printname arrow as our first function and then we'll print name function down here so we're both calling the arrow function and the normal function and printing them out and if we save this you'll see that the arrow function prints out Bob but the normal function doesn't print out any name and you are wondering why is that and that is because this is different inside of a function and inside of an arrow function so normally these standard functions syntax is here that we've been using since JavaScript came out it defines this based on where the function is called so as you can see the function is called down here print name function so this has the same scope as it would out here inside of this so if we did console dot log this dot name here you'll see as soon as that loads that it's also blank right there because this dot name is not defined in the global scope and this is a global scope to this because it's defined from what a function is called and that's a bit confusing to wrap your head around but it's easier to see how the print name arrow works because this is just the same as that this inside of this print name arrow function this does not actually get redefined when you use an arrow function and that's why this dot name is Bob inside of this set timeout for print named arrow and really that's the big difference is a normal standard function for JavaScript redefines this keyword so you can't actually use this dot name or anything else you defined on your this object inside of a normal function because it redefines it with whatever scope you call the function in and in our case that is the global scope that we're calling this function in but when you use an arrow function this is not redefined so this is going to be the exact same as this in the scope where the function is defined which is how it is in most other programming languages and it's such a relief to have this feature in JavaScript after so many years in almost all cases going for it I use arrow functions because it's much more intuitive to have this be defined in the scope that you call or that the function is defined rather than where you call the function because that just confuses me and confuses many other people so that's really the big reason to use an arrow function in my opinion is this scoping of this more similarly to how other programming languages work so I would recommend going forward to always use arrow functions unless you have a specific reason that you need this to be Reese code inside of that function which usually you'll not run into I hope you guys were able to learn the differences between arrow functions and standard functions and why arrow functions are almost always better for making your code more concise and legible and because the this context is defined more properly in arrow functions as opposed to standard functions if you guys did enjoy this video please make sure to subscribe for more videos just like this and check out my other videos listed over here which are going to be more JavaScript related content thank you guys very much for watching and have a good day

Original Description

ES6 added many amazing new features to JavaScript, but by far one of the best features is arrow functions. Arrow functions not only make code much more concise and legible, but it also handles the scope of this in a much more intuitive way. Arrow Functions Article: https://blog.webdevsimplified.com/2020-09/arrow-functions Twitter: https://twitter.com/DevSimplified GitHub: https://github.com/WebDevSimplified CodePen: https://codepen.io/WebDevSimplified #ES6 #ArrowFunctions #ModernJavaScript
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Web Dev Simplified · Web Dev Simplified · 43 of 60

1 Introduction to Web Development || Setup || Part 1
Introduction to Web Development || Setup || Part 1
Web Dev Simplified
2 Introduction to Web Development || Understanding the Web || Part 2
Introduction to Web Development || Understanding the Web || Part 2
Web Dev Simplified
3 Introduction to HTML || Your First Web Page || Part 1
Introduction to HTML || Your First Web Page || Part 1
Web Dev Simplified
4 Introduction to HTML || Basic HTML Elements || Part 2
Introduction to HTML || Basic HTML Elements || Part 2
Web Dev Simplified
5 Introduction to HTML || Advanced HTML Elements || Part 3
Introduction to HTML || Advanced HTML Elements || Part 3
Web Dev Simplified
6 Introduction to HTML || Links and Inputs || Part 4
Introduction to HTML || Links and Inputs || Part 4
Web Dev Simplified
7 Learn Git in 20 Minutes
Learn Git in 20 Minutes
Web Dev Simplified
8 5 Must Know Sites For Web Developers
5 Must Know Sites For Web Developers
Web Dev Simplified
9 10 Best Visual Studio Code Extensions
10 Best Visual Studio Code Extensions
Web Dev Simplified
10 Learn CSS in 20 Minutes
Learn CSS in 20 Minutes
Web Dev Simplified
11 How to Style a Modern Website (Part One)
How to Style a Modern Website (Part One)
Web Dev Simplified
12 How to Style a Modern Website (Part Two)
How to Style a Modern Website (Part Two)
Web Dev Simplified
13 3D Flip Button Tutorial
3D Flip Button Tutorial
Web Dev Simplified
14 How to Style a Modern Website (Part Three)
How to Style a Modern Website (Part Three)
Web Dev Simplified
15 Animated Loading Spinner Tutorial
Animated Loading Spinner Tutorial
Web Dev Simplified
16 How to Write the Perfect Developer Resume
How to Write the Perfect Developer Resume
Web Dev Simplified
17 Animated Text Reveal Tutorial
Animated Text Reveal Tutorial
Web Dev Simplified
18 Learn Flexbox in 15 Minutes
Learn Flexbox in 15 Minutes
Web Dev Simplified
19 Custom Checkbox Tutorial
Custom Checkbox Tutorial
Web Dev Simplified
20 Start Contributing to Open Source (Hacktoberfest)
Start Contributing to Open Source (Hacktoberfest)
Web Dev Simplified
21 JavaScript Shopping Cart Tutorial for Beginners
JavaScript Shopping Cart Tutorial for Beginners
Web Dev Simplified
22 Responsive Video Background Tutorial
Responsive Video Background Tutorial
Web Dev Simplified
23 1,000 Subscriber Giveaway
1,000 Subscriber Giveaway
Web Dev Simplified
24 How To Prevent The Most Common Cross Site Scripting Attack
How To Prevent The Most Common Cross Site Scripting Attack
Web Dev Simplified
25 Transparent Login Form Tutorial
Transparent Login Form Tutorial
Web Dev Simplified
26 The Forgotten CSS Position
The Forgotten CSS Position
Web Dev Simplified
27 How to Code a Card Matching Game
How to Code a Card Matching Game
Web Dev Simplified
28 10 Must Install Visual Studio Code Extensions
10 Must Install Visual Studio Code Extensions
Web Dev Simplified
29 Learn CSS Grid in 20 Minutes
Learn CSS Grid in 20 Minutes
Web Dev Simplified
30 Learn JSON in 10 Minutes
Learn JSON in 10 Minutes
Web Dev Simplified
31 10 Essential Keyboard Shortcuts For Programmers
10 Essential Keyboard Shortcuts For Programmers
Web Dev Simplified
32 What Is The Fastest Way To Load JavaScript
What Is The Fastest Way To Load JavaScript
Web Dev Simplified
33 Differences Between Var, Let, and Const
Differences Between Var, Let, and Const
Web Dev Simplified
34 How To Install MySQL (Server and Workbench)
How To Install MySQL (Server and Workbench)
Web Dev Simplified
35 Learn SQL In 60 Minutes
Learn SQL In 60 Minutes
Web Dev Simplified
36 How To Solve SQL Problems
How To Solve SQL Problems
Web Dev Simplified
37 What Are Design Patterns?
What Are Design Patterns?
Web Dev Simplified
38 Null Object Pattern - Design Patterns
Null Object Pattern - Design Patterns
Web Dev Simplified
39 Your First Node.js Web Server
Your First Node.js Web Server
Web Dev Simplified
40 How To Setup Payments With Node.js And Stripe
How To Setup Payments With Node.js And Stripe
Web Dev Simplified
41 How To Learn Any New Programming Skill Fast
How To Learn Any New Programming Skill Fast
Web Dev Simplified
42 Asynchronous Vs Synchronous Programming
Asynchronous Vs Synchronous Programming
Web Dev Simplified
JavaScript ES6 Arrow Functions Tutorial
JavaScript ES6 Arrow Functions Tutorial
Web Dev Simplified
44 Are You Too Old To Learn Programming?
Are You Too Old To Learn Programming?
Web Dev Simplified
45 JavaScript Cookies vs Local Storage vs Session Storage
JavaScript Cookies vs Local Storage vs Session Storage
Web Dev Simplified
46 JavaScript Promises In 10 Minutes
JavaScript Promises In 10 Minutes
Web Dev Simplified
47 Builder Pattern - Design Patterns
Builder Pattern - Design Patterns
Web Dev Simplified
48 JavaScript == VS ===
JavaScript == VS ===
Web Dev Simplified
49 JavaScript ES6 Modules
JavaScript ES6 Modules
Web Dev Simplified
50 8 Must Know JavaScript Array Methods
8 Must Know JavaScript Array Methods
Web Dev Simplified
51 CSS Variables Tutorial
CSS Variables Tutorial
Web Dev Simplified
52 JavaScript Async Await
JavaScript Async Await
Web Dev Simplified
53 How To Choose Your First Programming Language
How To Choose Your First Programming Language
Web Dev Simplified
54 Easiest Way To Work With Web Fonts
Easiest Way To Work With Web Fonts
Web Dev Simplified
55 Singleton Pattern - Design Patterns
Singleton Pattern - Design Patterns
Web Dev Simplified
56 Responsive Navbar Tutorial
Responsive Navbar Tutorial
Web Dev Simplified
57 CSS Progress Bar Tutorial
CSS Progress Bar Tutorial
Web Dev Simplified
58 Learn GraphQL In 40 Minutes
Learn GraphQL In 40 Minutes
Web Dev Simplified
59 What is an API?
What is an API?
Web Dev Simplified
60 Learn How To Build A Website In 1 Hour!
Learn How To Build A Website In 1 Hour!
Web Dev Simplified

This video tutorial teaches the basics of JavaScript ES6 arrow functions, including their syntax, usage, and scoping of the this keyword, with examples and code snippets to illustrate their application. By the end of this tutorial, viewers will be able to write concise and legible JavaScript code using arrow functions. The tutorial also covers the differences between arrow functions and standard functions, and how to apply arrow functions to real-world problems.

Key Takeaways
  1. Convert a normal function to an arrow function by removing the function keyword and assigning the function to a variable
  2. Remove the return statement and curly braces to reduce the arrow function to a single line of code
  3. Remove the parentheses around a single parameter in an arrow function
  4. Create a person object with a name
  5. Call the print name arrow function
  6. Call the print name function
💡 Arrow functions redefine the scoping of the this keyword, making it more intuitive and easier to use than standard functions

Related AI Lessons

Up next
Answering Revit Questions for Reddit
Balkan Architect
Watch →