Lädt...


🔧 Understanding Arrow Functions vs. Normal Functions in JavaScript


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

JavaScript gives us different ways to write functions, and two common methods are arrow functions and normal functions (often called "function declarations" or "function expressions"). Let’s break down the key differences between them in a simple way.

1. Normal Functions

Normal functions are the traditional way of writing functions in JavaScript. You can create them using the function keyword.

Example:

function add(a, b) {
  return a + b;
}

console.log(add(2, 3)); // Output: 5

Here, we define a function named add that takes two arguments, a and b, and returns their sum.

2. Arrow Functions

Arrow functions are a shorter way to write functions, introduced in ES6 (ECMAScript 2015). They use the => syntax.

Example:

const add = (a, b) => {
  return a + b;
};

console.log(add(2, 3)); // Output: 5

This arrow function does the same thing as the normal function above but is more concise.

Key Differences

1.Syntax:

  • Normal Function:

     function greet(name) {
       return `Hello, ${name}!`;
     }
    
  • Arrow Function:

     const greet = (name) => {
       return `Hello, ${name}!`;
     };
    

2.this Keyword:

  • In normal functions, this refers to the object that called the function. This can change depending on how the function is invoked.
  • Arrow functions, on the other hand, do not have their own this. They inherit this from the surrounding (lexical) context.

Example:

   const person = {
     name: 'Alice',
     sayName: function() {
       console.log(this.name);
     }
   };

   person.sayName(); // Output: "Alice"

If we used an arrow function:

   const person = {
     name: 'Alice',
     sayName: () => {
       console.log(this.name);
     }
   };

   person.sayName(); // Output: undefined

Here, this.name is undefined because arrow functions do not have their own this.

3.Arguments Object:

  • Normal functions have access to the arguments object, which is an array-like object containing the arguments passed to the function.
  • Arrow functions do not have their own arguments object.

Example:

   function showArguments() {
     console.log(arguments);
   }

   showArguments(1, 2, 3); // Output: [1, 2, 3]

Arrow function:

   const showArguments = () => {
     console.log(arguments);
   };

   showArguments(1, 2, 3); // Output: ReferenceError: arguments is not defined

4.Use Cases:

  • Normal Functions: Use when you need a function that requires its own this context or needs to access the arguments object.
  • Arrow Functions: Use for shorter functions or when you want to inherit this from the surrounding context.

Conclusion

Both arrow functions and normal functions have their place in JavaScript. Arrow functions are great for shorter, more concise code, while normal functions are better when you need more control over this or access to the arguments object. Understanding when to use each will help you write cleaner, more effective JavaScript code!

...

🔧 Understanding Arrow Functions vs. Normal Functions in JavaScript


📈 59.44 Punkte
🔧 Programmierung

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


📈 57.63 Punkte
🔧 Programmierung

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


📈 51.5 Punkte
🔧 Programmierung

🔧 The difference between Arrow functions and Normal functions in JavaScript


📈 51.5 Punkte
🔧 Programmierung

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


📈 38.49 Punkte
🔧 Programmierung

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


📈 38.49 Punkte
🔧 Programmierung

🔧 JavaScript Arrow Functions vs Regular Functions


📈 38.49 Punkte
🔧 Programmierung

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


📈 38.49 Punkte
🔧 Programmierung

🔧 Understanding JavaScript Arrow Functions


📈 37.79 Punkte
🔧 Programmierung

🔧 Understanding Arrow Functions in JavaScript: Advantages and Best Practices


📈 37.79 Punkte
🔧 Programmierung

🔧 Understanding the Role of Arrow Functions in JavaScript


📈 37.79 Punkte
🔧 Programmierung

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


📈 32.04 Punkte
🔧 Programmierung

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


📈 32.04 Punkte
🔧 Programmierung

🔧 7 Differences Between Arrow Functions and Traditional Functions


📈 32.04 Punkte
🔧 Programmierung

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


📈 29.85 Punkte
🎥 Video | Youtube

🔧 Mastering Arrow Functions in JavaScript


📈 29.85 Punkte
🔧 Programmierung

🔧 A Brief Intro to Arrow Functions in JavaScript


📈 29.85 Punkte
🔧 Programmierung

🔧 Arrow functions in JavaScript


📈 29.85 Punkte
🔧 Programmierung

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


📈 29.85 Punkte
🔧 Programmierung

🔧 Why you should not use Arrow Functions in JavaScript?


📈 29.85 Punkte
🔧 Programmierung

🔧 Easy JavaScript with Arrow Functions!


📈 29.85 Punkte
🔧 Programmierung

🔧 JavaScript | What Are Arrow Functions?


📈 29.85 Punkte
🔧 Programmierung

🔧 Anonymous and Arrow Functions in JavaScript


📈 29.85 Punkte
🔧 Programmierung

🔧 How to Use JavaScript Arrow Functions – Explained in Detail


📈 29.85 Punkte
🔧 Programmierung

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


📈 29.85 Punkte
🔧 Programmierung

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


📈 29.85 Punkte
🔧 Programmierung

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


📈 29.52 Punkte
📰 IT Security Nachrichten

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


📈 29.52 Punkte
📰 IT Security Nachrichten

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


📈 29.52 Punkte
🕵️ Sicherheitslücken

📰 Arrow ECS lädt zur »Arrow University«


📈 29.52 Punkte
📰 IT Security Nachrichten

🔧 Are React Hooks just normal javascript functions?


📈 28.1 Punkte
🔧 Programmierung

🐧 if we want linux to be used as a normal OS, we need to treat it like a normal OS


📈 26.02 Punkte
🐧 Linux Tipps

🔧 Functions of Commercial Bank: Primary Functions and Secondary Functions


📈 25.91 Punkte
🔧 Programmierung

matomo