# JavaScript Function
## Functions Assigned to Variables
```javascript=
let plusFive = (number) => {
return number + 5;
};
// f is assigned the value of plusFive
let f = plusFive;
plusFive(3); // 8
// Since f has a function value, it can be invoked.
f(9); // 14
```
In JavaScript, functions are a data type just as strings, numbers, and arrays are data types. Therefore, **functions can be assigned as values to variables**, but are different from all other data types because **they can be invoked**
## Callback Functions
```javascript=
const isEven = (n) => {
return n % 2 == 0;
}
let printMsg = (evenFunc, num) => {
const isNumEven = evenFunc(num);
console.log(`The number ${num} is an even number: ${isNumEven}.`)
}
// Pass in isEven as the callback function
printMsg(isEven, 4);
// Prints: The number 4 is an even number: True.
```
In JavaScript, a **callback function is a function that is passed into another function as an argument**. This function can then be invoked during the execution of that higher order function (that it is an argument of).
Since, in JavaScript, functions are objects, functions can be passed as arguments.
## Higher-Order Functions
In Javascript, functions can be assigned to variables in the same way that strings or arrays can. They can be **passed into other functions as parameters or returned from them as well**.
A “higher-order function” is a function that accepts functions as parameters and/or returns a function.
## Array Method `.reduce()`
```javascript=
const arrayOfNumbers = [1, 2, 3, 4];
const sum = arrayOfNumbers.reduce((accumulator, currentValue) => {
return accumulator + currentValue;
});
console.log(sum); // 10
```
The .reduce() method **iterates through an array and returns a single value**.
It takes a callback function with two parameters (accumulator, currentValue) as arguments. On each iteration, accumulator is the value returned by the last iteration, and the currentValue is the current element. Optionally, a second argument can be passed which acts as the initial value of the accumulator.
Here, the .reduce() method will sum all the elements of the array.
## Array Method `.forEach()`
```javascript=
const numbers = [28, 77, 45, 99, 27];
numbers.forEach(number => {
console.log(number);
});
```
The .forEach() method executes a callback function on each of the elements in an array in order.
Here, the callback function containing a console.log() method will be executed 5 times, once for each element.
## Array Method `.filter()`
```javascript=
const randomNumbers = [4, 11, 42, 14, 39];
const filteredArray = randomNumbers.filter(number => {
return number > 5;
});
```
The `.filter()` method **executes a callback function on each element in an array**. The callback function for each of the elements **must return either `true` or `false`**. **The returned array is a new array with any elements for which the callback function returns `true`**.
Here, the array `filteredArray` will contain all the elements of `randomNumbers` but `4`.
## Array Method `.map()`
```javascript=
const finalParticipants = ['Taylor', 'Donald', 'Don', 'Natasha', 'Bobby'];
const announcements = finalParticipants.map(member => {
return member + ' joined the contest.';
})
console.log(announcements);
```
The `.map()` method **executes a callback function on each element in an array**. It returns a new array made up of the return values from the callback function.
**The original array does not get altered**, and the returned array may contain different elements than the original array.
{"metaMigratedAt":"2023-06-15T21:55:58.978Z","metaMigratedFrom":"Content","title":"JavaScript Function","breaks":true,"contributors":"[{\"id\":\"bda81afc-f13e-4d82-9677-5359a2a615c9\",\"add\":3658,\"del\":49}]"}