JS Functions

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 11

JS_Functions

JavaScript Functions
A JavaScript function is a block of code designed to perform a particular task.

A JavaScript function is executed when "something" invokes it (calls it).

Function parameters are listed inside the parentheses () in the function definition.

Function arguments are the values received by the function when it is invoked.

Inside the function, the arguments (the parameters) behave as local variables.

function name(parameter1, parameter2, parameter3) {

// code to be executed

}
Why Functions?

You can reuse code: Define the code once, and use
it many times.

You can use the same code many times with


different arguments, to produce different results.
Function expression
The function keyword can be used to define a function inside an expression.

A function expression associates a value with a variable, just like any other
assignment statement. function expressions load only when the interpreter
reaches the definition of the function.

const getRectArea = function(width, height) {

return width * height;

};

console.log(getRectArea(3, 4));
Function Declaration
Function Declaration is the traditional way to define a function. It is somehow
similar to the way we define a function in other programming languages. We start
declaring using the keyword “function”. Then we write the function name and then
parameters.

// Function declaration
function add(a, b) {
console.log(a + b);
}
// Calling a function
add(2, 3);
Anonymous Function
Anonymous Function is a function that does not have any name associated
with it. Normally we use the function keyword before the function name to
define a function in JavaScript, however, in anonymous functions in
JavaScript, we use only the function keyword without the function name.
(function (){
// logic
})
let show = function() {
console.log('Anonymous function');
};
show();
Arrow Functions
Arrow functions are been introduced in the ES6 version of JavaScript. It is used to
shorten the code. Here we do not use the “function” keyword and use the arrow
symbol.
Furthermore, if the function body has only one statement that it returns, we can
skip curly braces {} and the return statement:
let name_of_function = (parameters) => ...

let add = (a, b) => a + b;

console.log(add(3, 2));
Map
The map() method is used for creating a new array from an existing one,
applying a function to each one of the elements of the first array.

const numbers = [1, 2, 3, 4];

const doubled = numbers.map(item => item * 2);

console.log(doubled); // [2, 4, 6, 8]
Filter
The filter() method takes each element in an array and it applies a
conditional statement against it. If this conditional returns true, the
element gets pushed to the output array. If the condition returns
false, the element does not get pushed to the output array.
const numbers = [1, 2, 3, 4];
const evens = numbers.filter(item => item % 2 ===
0);

console.log(evens); // [2, 4]
const students = [

{ name: 'Quincy', grade: 96 },

{ name: 'Jason', grade: 84 },

{ name: 'Alexis', grade: 100 },

{ name: 'Sam', grade: 65 },

{ name: 'Katie', grade: 90 }

];

const studentGrades = students.filter(student =>


student.grade >= 90);
Reduce
The reduce() method reduces an array of values down to just one
value. To get the output value, it runs a reducer function on each
element of the array.

const numbers = [1, 2, 3, 4];


const sum = numbers.reduce(function (result, item) {
return result + item;
});
console.log(sum);

You might also like