JavaScript functions are fundamental building blocks in web development. They allow you to encapsulate reusable code and perform specific tasks efficiently. Here's a breakdown of key concepts:

1. Function Declaration vs. Expression

2. Arrow Functions (ES6+)

  • Concise syntax for shorter functions:
    const add = (a, b) => a + b;  
    
    🔒 Understand closures and arrow functions
  • No own this context, ideal for callbacks:
    const numbers = [1, 2, 3];  
    numbers.forEach(num => console.log(num));  
    

3. Higher-Order Functions

4. Common Use Cases

  • Event Handlers:
    document.getElementById("button").addEventListener("click", () => {  
      alert("Button clicked!");  
    });  
    
  • Data Transformation:
    const doubled = [1, 2, 3].map(x => x * 2);  
    
  • Modular Code:
    function calculateArea(radius) {  
      return Math.PI * radius * radius;  
    }  
    

5. Best Practices


javascript_function
arrow_function
closure