Functions in
JavaScript
What is a function?
Parameters vs Arguments
Ways to implement a function
Function Declaration
Function Expression
Different types of function expressions
Named function expressions
Anonymous function expressions
Arrow function
IIFE
Higher Order Function
@adityauke
What is a function?
A function is a reusable piece of code that takes
some input, performs some tasks, and returns a
value. A function stores a piece of code that we want
to use repeatedly.
To use a function, we call that function by its name.
@adityauke
Parameters vs Arguments
When you define a function, the input values are
called parameters.
Parameters are something that is accepted by a
function.
When you invoke or call a function, the input values
are called arguments.
Arguments are something that is passed to a
function.
@adityauke
Ways to implement a function
Function Declaration
While defining a function if the first keyword of the
statement is a function keyword, then it will be called the
function declaration.
Function Expression
While defining a function if the first keyword of the
statement is not a function keyword, then it will be called
the function expression.
@adityauke
Different types of function expressions
Named function expressions
Anonymous function expressions
@adityauke
Different types of function expressions
Arrow function
Arrow functions were introduced in ES6.
1. If the arrow function has only one statement, you can
skip the curly braces and the return keyword.
2. if the arrow function has only one parentheses as well.
parameter, you can skip the
3. If the arrow function has only one statement and if it is
returning an object literal, use parentheses to wrap that
object literal otherwise it will not work as expected.
@adityauke
Different types of function expressions
Higher Order Function
A function that accepts functions as arguments and/or
returns a function is known as a Higher-order-function.
Array.map(), setTimeout(), and Array.sort() are examples
of Higher order Functions.
@adityauke
Different types of function expressions
IIFE - Immediately Invoked Function Expression
It is a function that is invoked immediately as soon as it is
defined without being explicitly called.
It is defined by wrapping the function in parentheses and
then adding a set of parentheses at the end to
immediately invoke it. We can pass arguments to the
additional set of parentheses.
If you will not write the additional set of parentheses at
the end, your function will not be invoked.
@adityauke