JavaScript Function Types Explained: Declarations, Expressions, and More

💛 JavaScript Functions (Part 2) — Types of Functions Explained Clearly 🔥 In JavaScript, functions are not all the same. How you define a function changes: ▪️hoisting behavior ▪️readability ▪️this binding ▪️how it’s used in real projects Let’s explore the most important types 👇 ♦️ Function Statement (Function Declaration) function greet() { console.log("Hello!"); } ✅ Key Points ▪️Fully hoisted ▪️Can be called before definition ▪️Stored completely during memory creation phase greet(); // works 💡 Best for general-purpose logic ♦️ Function Expression A function assigned to a variable. var add = function (a, b) { return a + b; }; ⚠️ Important ▪️Variable is hoisted as undefined ▪️Function is not hoisted add(); // ❌ TypeError 💡 Useful when functions behave like values ♦️ Anonymous Function A function without a name. setTimeout(function () { console.log("Hello"); }, 1000); 🧠 Key Idea ▪️Used where a function is needed temporarily ▪️Cannot exist on its own ▪️Mostly used as callbacks ♦️ Named Function Expression (NFE) A function expression with a name. const factorial = function fact(n) { if (n === 0) return 1; return n * fact(n - 1); }; ✅ Benefits ▪️Name is available inside the function only ▪️Helpful for recursion ▪️Better debugging (stack traces) fact(5); // ❌ not accessible outside ♦️ Arrow Functions 🏹 Introduced in ES6 for cleaner syntax. const multiply = (a, b) => a * b; 🔹 Differences from Normal Functions ▪️No own this ▪️No arguments object ▪️Lexically binds this Cannot be used as constructors const obj = { x: 10, show: () => console.log(this.x) }; obj.show(); // undefined ♦️ First-Class Functions 🥇 JavaScript treats functions as values. This means functions can: ✔️ Be assigned to variables ✔️ Be passed as arguments ✔️ Be returned from other functions 🔹 Example function greet() { return function () { console.log("Hello!"); }; } const sayHello = greet(); sayHello(); 👉 This is the foundation of: Callbacks, Closures and Higher-Order Functions 💡 Why This Matters Understanding function types helps you: ✅ Avoid hoisting bugs ✅ Use correct syntax in interviews ✅ Write cleaner, modern JS ✅ Understand React & async patterns 📌 This was Part 2 Next up 🔥 👉 Part 3: Higher-Order Functions, Callback Functions, IFFE & Real-World Use Cases If this helped you, drop a 💛 or share 🔁 #JavaScript #JSInternals #LearnJavaScript #WebDevelopment  #ProgrammingConcepts #WebDevJourney #BuildInPublic

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories