Javascript Functions
By Saket Bhatnagar••Beginner to Intermediate
functions
- 1Function is object.
- 2Function is a block of instruction which is used to perform a specific task.
- 3A function get executed only when it is called.
- 4The main advantage of function is we can achieve code reusability.
- 5To call a function we need its reference and ().
- 6Name of function is variable which holds the reference of function object.
- 7Creating a function using function keyword supports function hoisting.
- 8Therefore we can also call a function before function declaration.
- 9When we try to log function name the entire function defination is printed.
- 10The scope within function block is known as local scope.
- 11Any member with local scope cannot be used outside the function block.
- 12A parameter of function will have local scope.
- 13Variable written inside function even using var have local scope.
- 14Inside a function we can use the members of global scope.
- 15In javascript 'this' is a property of every function.(every function will have 'this' Keyword except arrow function)
parameter
- 1The variables declared in the function defination is known as parameters.
- 2The parameters have local scope (can be used only inside function body).
- 3Parameters are used to hold the values passed by caller (or calling statement).
arguments
- 1The values passed in the method call statement is known as arguments.
- 2Note : An argument can be a literal, variable or an expression which gives a results.
return keyword
- 1It is a keyword used as control transfer statement in a function.
- 2Return will stop the execution of the function and transfer control along with data to the caller.
ways to create Functions
Function declaration statement : Create using function keyword
- 1
Syntax :
1function func_variable(parameters) {2 //statements3}4func_variable() - 2
Example : Create a function 'greet' which should print a message "Good Morning" when it is called.
1function func_variable(parameters) {2 //statements3}4func_variable() - 3
Function can be Hoisted.
1//Here, we are accessing function before it's declaration statement.23greet();4function greet() {5 console.log("Good Morning");6}78output : Good Morning - 4Function does not belongs to temporal dead zone.
- 1
Function as expression / expression function
- 1Function which is passed to an variable as a value is called as first class function.
- 2 Function can not be Hoisted because it is object is created in execution phase.
- 3Function does not belongs to temporal dead zone
Functional Programming
- 1 Functional Programming is a programming technique where we pass a function along with a value to another function.
- 2In this approach, we generate Generic Function. Here function task is not predefined. It perform multiple task not only single task
- 3The Function which accept another function as a parameter or return a function is known as 'Higher Order Function'.
- 4The Function which is passed to another function or the function which is returned by another function is known as 'Callback Function'.
Types of Functions
Function decalaration statement : Using function keyword
Function as expression / expression function
Immediate Invoke Function (IIF)
- 1when a function is called as soon as it's object is created is known as Immediate Invoke Function.
- 2We have to write the function inside the paranthesis to group it. [using Group operator -> (function code) ].
- 3The function is not visible(available) outside the scope.
- 4After grouping it, we have to use paranthesis to call this function.
- 5Immediate Invoke Function execute only once.
Arrow Function
- 1The main function of arrow function is to reduce the function syntax.
- 2Arrow Function is introduced in ES6.
- 3If we have only single parameter, it is not necessary to use paranthesis for paramenter.
- 4If function have single statement, then block (curly braces) is optional.
- 5It does not have its own 'this' property.
- 6 Implicit return :- If there is only one statement and If block is not created then JS Engine will return that statement automatically.
- 7 Explicit return :- If block is created and function is not returning any value, JS Engine will return undefined. To return a value Explicitly from block, we have to use return keyword.If block is created then we have to use return keyword to return value otherwise JS Engine will return undefined.
Higher Order Function
- 1The Function which accept another function as a parameter or return a function is known as 'Higher Order Function'.
Callback Function
- 1The Function which is passed to another function or the function which is returned by another function is known as 'Callback Function'.
Nested Function
- 1The function inside another function is called as nested function.
- 2
Example :
1function outer(){2 function inner(){3 }4} - 3The outer function is known as parent and the inner function is known as child.
- 4The inner function is local to outer function, it cannot be accessed from outside.
- 5
To use inner function outside, the outer function must return the reference of inner function.
1function outer(){2 function inner(){3 }4}
We can now call inner function from outside as follows:
1st Way:1let fun=outer();2fun(); // -----> inner() is called
2nd Way:1outer()(); // -----> inner() is called