Functions are set of statements that perform a task. The Function keyword can be used to define a function inside the expression. These can be defined in two ways i.e. function constructor or function expression.
Function Expression :
Functions can be created by function expression. These functions are known as anonymous function i.e. it doesn't have any change.
Syntax :
var myFunc = function [name]([param1[,param2[,...,paramN]]]){
//statements
}
Parameters :
name : name is the function name and this is not mandatory. So, it can be omitted, in this case it will be an anonymous function.
params : params are the name of the argument which are been passed to the function.
statements : The statements will be body of the function.
Examples :
var a = function(a) {
console.log(a);
}
a(1);
output :
1
Function Expression are convenient when passing a function as an argument to another function.
Example :
function firstFunc(anotherfunc){
anotherfunc();
}
function secondFunc(){
console.log("secondFunc() is passed as an argument to the firstFunc()");
}
firstFunc(secondFunc);
output :
secondFunc() is passed as an argument to the firstFunc()
Function Declaration :
Function declaration is also known as function definition or function statement.
A Function which is been created with a function declaration will be a function object and it has all the properties, methods, behavior of function objects. By default the return type of function is undefined and to return any value the function must have a return statement that will specify the value to be returned.
It consists of :
function a(){
console.log("hi");
}
a();
output :
hi
If the object is passed as a parameter and the function changes it's object properties then change is effected even outside the function.
Example :
function first(a){
a.name = "john";
}
var details = { name:'joy', gender:'female' };
var x,y;
console.log(details.name);
first(details);
console.log(details.name);
output :
joy
john
The main difference between function expression and function statement :
b(1);
Naming Conflict :
When two variables or arguments have the same name then it would be a name conflict. Inner scope will have the highest precedence and the outer scope will have the least precedence. This is scope chain.
Example :
function outer(){
var x = 10;
function inner(x){
return x * 10;
}
return inner;
}
outer()(20);
output :
200
Here, the name conflict is the variable x. So, the inner x will take the scope the outer x and the output will be 200 instead of 100.
Function Expression :
Functions can be created by function expression. These functions are known as anonymous function i.e. it doesn't have any change.
Syntax :
var myFunc = function [name]([param1[,param2[,...,paramN]]]){
//statements
}
Parameters :
name : name is the function name and this is not mandatory. So, it can be omitted, in this case it will be an anonymous function.
params : params are the name of the argument which are been passed to the function.
statements : The statements will be body of the function.
Examples :
var a = function(a) {
console.log(a);
}
a(1);
output :
1
Function Expression are convenient when passing a function as an argument to another function.
Example :
function firstFunc(anotherfunc){
anotherfunc();
}
function secondFunc(){
console.log("secondFunc() is passed as an argument to the firstFunc()");
}
firstFunc(secondFunc);
output :
secondFunc() is passed as an argument to the firstFunc()
Function Declaration :
Function declaration is also known as function definition or function statement.
A Function which is been created with a function declaration will be a function object and it has all the properties, methods, behavior of function objects. By default the return type of function is undefined and to return any value the function must have a return statement that will specify the value to be returned.
It consists of :
- name of the function.
- list of parameters that is been passed inside the parenthesis separated by comma.
- body which is been enclosed in the curly braces {}.
function a(){
console.log("hi");
}
a();
output :
hi
If the object is passed as a parameter and the function changes it's object properties then change is effected even outside the function.
Example :
function first(a){
a.name = "john";
}
var details = { name:'joy', gender:'female' };
var x,y;
console.log(details.name);
first(details);
console.log(details.name);
output :
joy
john
The main difference between function expression and function statement :
- function statement is the function name which can be omitted in the function expression which will be defined as anonymous function.
- This function Expression can be used as Immediately Invoked Function Expression. As, it can run as soon as it is been defined.
- Function Expression are not hoisted as Function declaration.
Example :
var b = function(a) {
console.log(a);
}
console.log(a);
}
It throws an error by saying that prototype of undefined.
Named Function Expression :
To refer a current function inside the function we create a named function expression. This name is accessible only to the function body scope and this also avoids non-standard property.
Anonymous function Example :
var a = function(b){
console.log(b + b);
}
a(5);
output :
10
Naming Conflict :
When two variables or arguments have the same name then it would be a name conflict. Inner scope will have the highest precedence and the outer scope will have the least precedence. This is scope chain.
Example :
function outer(){
var x = 10;
function inner(x){
return x * 10;
}
return inner;
}
outer()(20);
output :
200
Here, the name conflict is the variable x. So, the inner x will take the scope the outer x and the output will be 200 instead of 100.
No comments:
Post a Comment