𝗙𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝗶𝗻 𝗝𝗮𝘃𝗮𝗦𝗰𝗿𝗶𝗽𝘁
Functions help you group code into reusable blocks. You need to know the different types to write better code.
Here are the main types of functions:
• Named Functions These use a specific name. function greet() { return "Hello!"; }
• Anonymous Functions These do not have a name. const greet = function() { return "Hi!"; };
• Function Expressions You store a function inside a variable. const add = function(a, b) { return a + b; };
• Arrow Functions A shorter syntax introduced in ES6. const square = n => n * n;
• IIFE (Immediately Invoked Function Expression) This runs as soon as you define it. (function () { console.log("Runs now!"); })();
• Async Functions These handle asynchronous operations. async function fetchData() { return "Data ready!"; }
• Pure Functions These return the same result for the same inputs and have no side effects. function pureAdd(a, b) { return a + b; }
Other important types include:
- Callback Functions
- Constructor Functions
- Generator Functions
- Recursive Functions
- Higher-Order Functions
- Nested Functions
- Rest Parameter Functions
Mastering these will improve your coding skills.
Source: https://www.geeksforgeeks.org/javascript/functions-in-javascript/ Full post: https://dev.to/madhanraj/functions-in-javascript-3k17