Skip to content

Commit 6267bd0

Browse files
committed
Added functions
1 parent 78456e9 commit 6267bd0

File tree

2 files changed

+97
-6
lines changed

2 files changed

+97
-6
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,12 @@ This repository contains Vanilla JavaScript topics and their notes to learn from
5050
- [while loop](./docs/control-flow.md#-while-loop)
5151
- [do while loop](./docs/control-flow.md#-do-while-loop)
5252
- [comparison](./docs/control-flow.md#-comparison)
53-
2. Functions:
54-
- Defining functions
55-
- Calling functions
56-
- Function arguments and return values
57-
- Function Scope and closures
58-
- Arrow functions
53+
2. [Functions:](./docs/functions.md)
54+
- [Defining functions](./docs/functions.md#-defining-functions)
55+
- [Calling functions](./docs/functions.md#-calling-functions)
56+
- [Function arguments and return values](./docs/functions.md#-function-arguments-and-return-values)
57+
- [Function Scope and closures](./docs/functions.md#-function-scope-and-closures)
58+
- [Arrow functions](./docs/functions.md#-arrow-functions)
5959
3. DOM Manipulation:
6060
- Selecting elements
6161
- Creating elements

docs/functions.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
## ⚑ Functions
2+
Functions are reusable blocks of code that perform specific tasks. It helps to organize the code, make it more modular, and for code reusability.
3+
4+
### ☴ Overview:
5+
1. [Defining functions](#-defining-functions)
6+
2. [Calling functions](#-calling-functions)
7+
3. [Function arguments and return values](#-function-arguments-and-return-values)
8+
4. [Function Scope and closures](#-function-scope-and-closures)
9+
5. [Arrow functions](#-arrow-functions)
10+
11+
### ✦ Defining Functions:
12+
The functions are usable before defining it.
13+
14+
*Syntax:*
15+
```javascript
16+
function functionName(parameter1, parameter2, ...) {
17+
// Function body (statements to be executed)
18+
return value; // Optional: Returns a value
19+
}
20+
```
21+
22+
```javascript
23+
function greet(name) {
24+
console.log("Hello, " + name + "!");
25+
}
26+
```
27+
28+
### ✦ Calling Functions:
29+
To use function in the JavaScript code, that need to be called where it is required.
30+
31+
*Syntax:*
32+
```javascript
33+
functionName(argument1, argument2, ...);
34+
```
35+
36+
```javascript
37+
greet("Kumar"); // Output: Hello, Kumar!
38+
```
39+
40+
### ✦ Function Arguments and Return Values:
41+
**Arguments:** Values passed to a function that accepts as parameter of it when it's called.
42+
**Return Values:** Values that a function can return to the where it was called or invoked using the return statement in the function.
43+
44+
```javascript
45+
function add(a, b) {
46+
return a + b;
47+
}
48+
49+
let sum = add(1, 2);
50+
console.log(sum); // Output: 3
51+
```
52+
53+
### ✦ Function Scope and Closures:
54+
**Scope:** The region of code where a variable or function is accessible.
55+
**Closures:** Functions that have access to variables from the outer scope, even after the outer scope has finished executing.
56+
57+
```javascript
58+
function outerFunction() {
59+
let x = 10;
60+
61+
function innerFunction() {
62+
console.log(x); // Accesses the outer variable x
63+
}
64+
65+
return innerFunction;
66+
}
67+
68+
let closure = outerFunction();
69+
closure(); // Output: 10
70+
```
71+
72+
### ✦ Arrow Functions:
73+
It is defined in a concise way or in shorthand form.
74+
75+
*Syntax:*
76+
```JavaScript
77+
(parameters) => expression;
78+
```
79+
80+
```javascript
81+
let greet = (name) => {
82+
console.log("Hello, " + name + "!");
83+
};
84+
```
85+
86+
---
87+
[⇪ To Top](#-functions)
88+
89+
[❮ Previous Topic](./control-flow.md)   [Next Topic ❯](./dom-manipulation.md)
90+
91+
[⌂ Goto Home Page](../README.md)

0 commit comments

Comments
 (0)