Skip to content

Commit 6fce392

Browse files
JS functions
1 parent 58756b9 commit 6fce392

File tree

3 files changed

+226
-0
lines changed

3 files changed

+226
-0
lines changed

functions/functions.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// function statement
2+
// Function statements are hoisted, meaning they can be called before they are defined in the code.
3+
// function can be assigned to a variable, passed as an argument, or returned from another function.
4+
// Function statements are also known as function declarations.
5+
function a() {
6+
console.log("Function Statement or Declaration");
7+
}
8+
9+
a(); // Calling the function statement
10+
11+
12+
//function expression
13+
// Function expressions are not hoisted, so they must be defined before they are called.
14+
15+
var b = function() {
16+
console.log("Function Expression");
17+
}
18+
b(); // Calling the function expression
19+
20+
// difference between function statement and function expression is that function statements are hoisted, while function expressions are not.
21+
// Function expressions can be anonymous or named, while function statements are always named.
22+
// Function expressions can be assigned to variables, passed as arguments, or returned from other functions.
23+
// Function statements are defined using the `function` keyword followed by a name, while function expressions can be anonymous or named.
24+
// Function declaration is a named function that is defined using the `function` keyword.
25+
26+
27+
28+
29+
30+
//Anonymous function
31+
// Anonymous functions are function expressions that do not have a name.
32+
// They are often used as arguments to other functions or as immediately invoked function expressions (IIFE
33+
// anonymous function are used as values, such as in callbacks or event handlers.
34+
35+
var c = function() {
36+
console.log("Anonymous Function");
37+
}
38+
39+
setTimeout(c, 2000); // Calling the anonymous function after 1 second
40+
41+
//Named function Expression
42+
var d = function namedFunction() {
43+
console.log("Named Function Expression");
44+
}
45+
46+
d(); // Calling the named function expression
47+
// namedFunction(); // Calling the named function expression by its name will throw an error because it is not hoisted like a function declaration.
48+
// referemce error: namedFunction is not defined
49+
50+
setTimeout(d, 3000); // Calling the named function expression after 3 seconds
51+
52+
//Differece between arguments and parameters ?
53+
// Parameters are the variables listed as part of a function's definition.
54+
// Arguments are the actual values that are passed to the function when it is called.
55+
// Parameters are used to define the inputs that a function can accept, while arguments are the actual values passed to those parameters when the function is invoked.
56+
57+
58+
// First class functions
59+
60+
// In JavaScript, functions are first-class citizens, meaning they can be treated like any other value.
61+
// They can be assigned to variables, passed as arguments to other functions, returned from functions,
62+
// and stored in data structures like arrays and objects.
63+
// This allows for powerful programming patterns like higher-order functions, callbacks, and function composition.
64+
// Example of first-class functions
65+
66+
function firstClassFunctionExample() {
67+
console.log("This is a first-class function.");
68+
}
69+
70+
firstClassFunctionExample(); // Calling the first-class function
71+
72+
//Arrow functions
73+
74+
// Arrow functions are a concise way to write function expressions in JavaScript.
75+
// They use the `=>` syntax and do not have their own `this`, making them particularly useful for callbacks and methods that require the context of the surrounding scope.
76+
const arrowFunctionExample = () => {
77+
console.log("This is an arrow function.");
78+
};
79+
80+
arrowFunctionExample(); // Calling the arrow function

functions/functions.md

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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.

functions/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Functions in JavaScript</title>
7+
<h1>Functions in JavaScript</h1>
8+
<script src="functions.js"></script>
9+
</head>
10+
</html>

0 commit comments

Comments
 (0)