Day 11
Day 11
Day 11
AND METHODS
IN JAVASCRIPT
1
• Types of functions in JS
• How JS treat function (first-class-citizens)
• What are callbacks ?
AGENDA • What are higher-order functions
Types Of Functions ?
1. Named Function
2. Anonymous Function
3. Arrow Function
Named Function
Steps Needed For Function ?
function greet(name) {
return "Hello, " + name + "!";
}
console.log(result); // Outputs: 8
Arrow Function
The Arrow Function
Arrow functions were introduced with ES6 as a new syntax for writing
JavaScript functions
Thus we can say that Arrow Functions save a developer’s time and
simplify code development
General Syntax
Points to understand:
1. No function keyword
2. No function name
3. A list of arguments within parenthesis, followed by a
'fat arrow' (=>), followed by a function body.
Example
The same can be done in a more concise way using an Arrow Function
Let’s see how…
Example
If the Arrow Function has only one statement, and the statement
returns a value, we can remove the braces as well as the
return keyword:
For example:
If the Arrow Function has only one parameter, then we can skip the
parentheses as well:
For example:
Properties and Methods: Since functions are objects, they can have
properties and methods just like any other object. For example,
functions have a length property that indicates the number of named
arguments they expect, and they also have methods like bind, call, and
apply.
Example:-
function add(a, b) {
return a + b;
}
Output:-
In JavaScript, all functions are objects, just like Array , Math, Date etc
<function_1>(<some_arg>,<function_2>);
In the above syntax , we are calling function_1() with some data and the
name of another function called function_2 as argument.
function function_1(arg,function_2){
// initial code
function_2();
}
Example
A regular JS Function:
function multiply(a, b) {
var result = a * b;
console.log("multiply Function Result:",result);
}
multiply(2, 4);
However, there are some cases that a piece of code must run after
something else happens and also not sequentially.
This is called Asynchronous Programming. That we will cover while covering later
in the course.
Callbacks make sure that a function is not going to run before a task is
completed but will run right after the task has completed.
so all parent function that take callback function as an argument are higher
order function (HOD).
• forEach()
• map()
• filter()
• etc.…
foreach()
foreach()
So, what is forEach? At its core, forEach is a method available for arrays in
JavaScript.
It's designed to iterate over each element in an array and apply a provided
function (often referred to as a "callback") to each of those elements.
This means we can customize what happens to each item in the array without
writing a traditional for loop.
forEach syntax
numbers.forEach(function(currentValue) {
doubled.push(currentValue * 2);
});
Example:-
var numbers = [1, 2, 3, 4, 5];
The syntax for filter is straightforward and same as foreach and map:
4. One important rule to remember: the rest parameter always comes at the end in the
function syntax. This means that you define regular parameters first, and then the rest
parameter, which collects all remaining arguments. This order is crucial for JavaScript to
correctly interpret the function's behavior.
Unleashing the Power of Flexibility
The beauty of the rest operator lies in its ability to handle an arbitrary
number of arguments gracefully. Consider a scenario where you want
to create a function that calculates the sum of any number of values:
Example:-
function calculateSum(...numbers) {
return numbers.reduce((acc, current) => acc + current, 0);
}
console.log(result); // Outputs: 15