Lädt...


🔧 Arrow functions in JavaScript


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Introduction

Arrow functions are a type of function in JavaScript that have no name which is why they are called anonymous functions and if you want them to have a name that’s callable you would have to assign them to a variable.

Basic Syntax

Single-Line Arrow Functions

An arrow function can just be a line of code, and in arrow functions you can omit the curly bracket and the return keyword if it’s a single line. This way of returning a value in a single line arrow function is called an implicit return
Single-Link Example
if there’s a single parameter, This code snippet is a function that just returns the name. You can omit the bracket and just use the name of the parameter.
Omitting bracket
If there’s no parameter, you can omit the bracket. This code doesn't require a parameter, which means you can just use the underscore symbol ( _ )
Using underscore

Multi-Line Arrow Functions

Most functions you will write will fall into this category of a multi-line arrow function. Just like a regular function, we have to use the curly brackets and the return keyword to return a value. When you use the return keyword, it’s called an explicit return
Multi-line

Comparison with Traditional Functions

Syntax Differences

Arrow Function

In single line format

single line format Syntax

In Multi-line format

Multi-line format Syntax

Regular Function

Regular Function Syntax

Behaviour Differences

  1. Arrow functions don’t have their own context (this) so they inherit it from the part of the code they were written and not where they were called. We call this lexical scoping.

    Example:
    code example of lexical scoping

    We have two arrow functions, one defined in a regular function and the other in the object's root. The arrFunc() is undefined even if it was defined in obj. That’s because arrow functions don’t have access to this context of obj so it will look for it in the global scope and since it didn’t find a name, it returns undefined.

    But the arrFuncInRegFunc returns John, that’s because it’s inheriting this from the function it’s defined in and not the obj.

  2. Arrow functions don’t have their argument object, if you try to access the argument object of an arrow function it will empty

    Example in global scope:

    Example in global scope

    Example in a regular function:

    Example in regular function

    Here the arrow function inherits the arguments object from the regular function because it doesn’t have access to its argument object

  3. Arrow functions cannot be used as constructors and will throw an error if used with the new keyword

    lack of new keyword code example

    Example of using constructors with regular functions:

constructors in reg functions code example

Practical Example

Arrow functions are mostly used as callback functions to other functions, here are a few examples:

In Array Methods

  1. Map method

Arrow function in map method

  1. Filter method

Arrow function in filter method

In Event Handlers

Arrow function in event handlers

Conclusions

Arrow functions were a great introduction to ES6 they allow for concise syntax because they are shorter and more compact than regular function expressions, making them easier to read and write, and also have implicit returns for single-line functions, so you don't need to use the return keyword if the value is directly after the arrow ⇾. However, if you use curly brackets, you must have a return statement or the function will return undefined.

You can read more about arrow functions here

Thanks for reading, let me know what you think about this and if you would like to see more, if you think i made a mistake or missed something, don't hesitate to comment

...

🔧 JavaScript Regular/Normal vs Arrow Function: My Beef with Arrow Functions.


📈 46.13 Punkte
🔧 Programmierung

🔧 🚀 JavaScript Functions: Arrow Functions, Callbacks, and Closures 📜


📈 41.95 Punkte
🔧 Programmierung

🔧 Understanding Arrow Functions vs. Normal Functions in JavaScript


📈 41.95 Punkte
🔧 Programmierung

🔧 Arrow Functions vs. Regular Functions in JavaScript: A Showdown


📈 41.95 Punkte
🔧 Programmierung

🔧 Mastering JavaScript Functions: Your Guide to Normal vs. Arrow Functions


📈 41.95 Punkte
🔧 Programmierung

🔧 JavaScript Arrow Functions vs Regular Functions


📈 41.95 Punkte
🔧 Programmierung

🔧 Arrow Functions vs. Regular Functions in JavaScript: A Comprehensive Guide


📈 41.95 Punkte
🔧 Programmierung

🔧 The difference between Arrow functions and Normal functions in JavaScript


📈 41.95 Punkte
🔧 Programmierung

🔧 Why the "this" Keyword Behaves Differently in Regular Functions and Arrow Functions


📈 35.54 Punkte
🔧 Programmierung

🔧 7 Differences Between Arrow Functions and Traditional Functions


📈 35.54 Punkte
🔧 Programmierung

🔧 Draft: What are the differences between arrow functions and traditional functions?


📈 35.54 Punkte
🔧 Programmierung

🔧 Mastering Arrow Functions in JavaScript


📈 31.5 Punkte
🔧 Programmierung

🔧 A Brief Intro to Arrow Functions in JavaScript


📈 31.5 Punkte
🔧 Programmierung

🔧 Arrow functions in JavaScript


📈 31.5 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Arrow Functions


📈 31.5 Punkte
🔧 Programmierung

🔧 JavaScript: forEach, map, Arrow Functions, setTimeout, setInterval, filter, some, every, and reduce


📈 31.5 Punkte
🔧 Programmierung

🔧 Understanding Arrow Functions in JavaScript: Advantages and Best Practices


📈 31.5 Punkte
🔧 Programmierung

🔧 Why you should not use Arrow Functions in JavaScript?


📈 31.5 Punkte
🔧 Programmierung

🔧 Easy JavaScript with Arrow Functions!


📈 31.5 Punkte
🔧 Programmierung

🔧 JavaScript | What Are Arrow Functions?


📈 31.5 Punkte
🔧 Programmierung

🔧 Anonymous and Arrow Functions in JavaScript


📈 31.5 Punkte
🔧 Programmierung

🔧 How to Use JavaScript Arrow Functions – Explained in Detail


📈 31.5 Punkte
🔧 Programmierung

🔧 🚀How JavaScript Works (Part 9)? Arrow functions and lexical this


📈 31.5 Punkte
🔧 Programmierung

🔧 Understanding the Role of Arrow Functions in JavaScript


📈 31.5 Punkte
🔧 Programmierung

🔧 Arrow Functions in JavaScript: How to Use Fat & Concise Syntax


📈 31.5 Punkte
🔧 Programmierung

🎥 Demo: Arrow and anonymous functions [40 of 51] | Beginner's Series to JavaScript


📈 31.5 Punkte
🎥 Video | Youtube

🔧 Functions of Commercial Bank: Primary Functions and Secondary Functions


📈 31.36 Punkte
🔧 Programmierung

📰 Arrow Forum 2024: Arrow feiert ein großes Familienfest - channelpartner.de


📈 29.26 Punkte
📰 IT Security Nachrichten

📰 Channel-Treff in München: Arrow veranstaltet Arrow University - channelpartner.de


📈 29.26 Punkte
📰 IT Security Nachrichten

🕵️ arrow-kt Arrow up to 0.8.x Gradle Build Artifact Resolver weak encryption


📈 29.26 Punkte
🕵️ Sicherheitslücken

📰 Arrow ECS lädt zur »Arrow University«


📈 29.26 Punkte
📰 IT Security Nachrichten

🔧 You Need to Know About Pure Functions & Impure Functions in JavaScript


📈 27.33 Punkte
🔧 Programmierung

🔧 A Comprehensive Guide to ES6 and Arrow Functions


📈 25.08 Punkte
🔧 Programmierung

🔧 Day 59 / 100 Days of Code: Reflecting on Arrow Functions


📈 25.08 Punkte
🔧 Programmierung

matomo