|
| 1 | +# JavaScript Functions: Concepts and Examples |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 1. Function Statement (Function Declaration) |
| 8 | +A function statement is also known as a function declaration. It is hoisted, so you can call it before its definition in the code. |
| 9 | + |
| 10 | +```js |
| 11 | +function a() { |
| 12 | + console.log("Function Statement or Declaration"); |
| 13 | +} |
| 14 | + |
| 15 | +a(); // Calling the function statement |
| 16 | +``` |
| 17 | + |
| 18 | +**Key Points:** |
| 19 | +- Hoisted (can be called before definition) |
| 20 | +- Always named |
| 21 | +- Cannot be used as a value directly |
| 22 | + |
| 23 | +--- |
| 24 | + |
| 25 | +## 2. Function Expression |
| 26 | +A function expression is created when a function is assigned to a variable. Function expressions are not hoisted, so they must be defined before use. |
| 27 | + |
| 28 | +```js |
| 29 | +var b = function() { |
| 30 | + console.log("Function Expression"); |
| 31 | +} |
| 32 | +b(); // Calling the function expression |
| 33 | +``` |
| 34 | + |
| 35 | +**Key Points:** |
| 36 | +- Not hoisted |
| 37 | +- Can be anonymous or named |
| 38 | +- Can be assigned to variables, passed as arguments, or returned from other functions |
| 39 | + |
| 40 | +--- |
| 41 | + |
| 42 | +## 3. Anonymous Function |
| 43 | +An anonymous function is a function without a name. It is often used as a value, such as in callbacks or event handlers. |
| 44 | + |
| 45 | +```js |
| 46 | +var c = function() { |
| 47 | + console.log("Anonymous Function"); |
| 48 | +} |
| 49 | +setTimeout(c, 2000); // Calling the anonymous function after 2 seconds |
| 50 | +``` |
| 51 | + |
| 52 | +**Key Points:** |
| 53 | +- No name |
| 54 | +- Used as values, callbacks, or arguments |
| 55 | + |
| 56 | +--- |
| 57 | + |
| 58 | +## 4. Named Function Expression |
| 59 | +A named function expression is a function expression with a name. The name is only accessible inside the function itself. |
| 60 | + |
| 61 | +```js |
| 62 | +var d = function namedFunction() { |
| 63 | + console.log("Named Function Expression"); |
| 64 | +} |
| 65 | +d(); // Works |
| 66 | +// namedFunction(); // ReferenceError: namedFunction is not defined |
| 67 | +setTimeout(d, 3000); // Calling after 3 seconds |
| 68 | +``` |
| 69 | + |
| 70 | +**Key Points:** |
| 71 | +- Name is local to the function body |
| 72 | +- Not hoisted |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +## 5. Difference Between Arguments and Parameters |
| 77 | +- **Parameters** are variables listed in the function definition. |
| 78 | +- **Arguments** are the actual values passed to the function when it is called. |
| 79 | + |
| 80 | +```js |
| 81 | +function sum(a, b) { // a, b are parameters |
| 82 | + return a + b; |
| 83 | +} |
| 84 | +sum(2, 3); // 2, 3 are arguments |
| 85 | +``` |
| 86 | + |
| 87 | +--- |
| 88 | + |
| 89 | +## 6. First-Class Functions |
| 90 | +In JavaScript, functions are first-class citizens. This means: |
| 91 | +- They can be assigned to variables |
| 92 | +- Passed as arguments |
| 93 | +- Returned from other functions |
| 94 | +- Stored in data structures |
| 95 | + |
| 96 | +```js |
| 97 | +function firstClassFunctionExample() { |
| 98 | + console.log("This is a first-class function."); |
| 99 | +} |
| 100 | +firstClassFunctionExample(); |
| 101 | +``` |
| 102 | + |
| 103 | +**Key Points:** |
| 104 | +- Enables higher-order functions, callbacks, and function composition |
| 105 | + |
| 106 | +--- |
| 107 | + |
| 108 | +## 7. Arrow Functions |
| 109 | +Arrow functions are a concise way to write function expressions. They do not have their own `this` and are best for callbacks and methods that use the surrounding context. |
| 110 | + |
| 111 | +```js |
| 112 | +const arrowFunctionExample = () => { |
| 113 | + console.log("This is an arrow function."); |
| 114 | +}; |
| 115 | +arrowFunctionExample(); |
| 116 | +``` |
| 117 | + |
| 118 | +**Key Points:** |
| 119 | +- Shorter syntax |
| 120 | +- No own `this`, `arguments`, or `super` |
| 121 | +- Cannot be used as constructors |
| 122 | + |
| 123 | +--- |
| 124 | + |
| 125 | +## Summary Table |
| 126 | +| Concept | Hoisted | Named | Can be Anonymous | Can be Value | Example Use Case | |
| 127 | +|--------------------------|---------|-------|------------------|--------------|-------------------------| |
| 128 | +| Function Statement | Yes | Yes | No | No | Declarations | |
| 129 | +| Function Expression | No | Yes/No| Yes | Yes | Callbacks, assignments | |
| 130 | +| Anonymous Function | No | No | Yes | Yes | Callbacks, IIFE | |
| 131 | +| Named Function Expression| No | Yes | No | Yes | Recursion, debugging | |
| 132 | +| Arrow Function | No | No | Yes | Yes | Callbacks, short syntax | |
| 133 | + |
| 134 | +--- |
| 135 | + |
| 136 | +**Practice:** Try modifying the examples and see how hoisting, naming, and context affect function behavior in JavaScript. |
0 commit comments