๐๐๐ป๐ฐ๐๐ถ๐ผ๐ป๐ ๐ถ๐ป ๐๐ฎ๐๐ฎ๐ฆ๐ฐ๐ฟ๐ถ๐ฝ๐
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