Function is basically used to reusable the code. A function is executed when you call it.
Syntax:-
function functionName(parameters){
// logic implement
}
Example:- Suppose you define employeeDetails function which has two parameters name and age.
function employeeDetails(name, age){
console.log("my name is "+name+" and my age is "+age);
}
How to call a function?
You have already defined the function after that you can call the function multiple times based on your requirements.
employeeDetails("John",35); //my name is John and my age is 35
employeeDetails("Rom",30); //my name is Rom and my age is 30
What is Function Scoping?
Everything defined in the function can not be access outside the function, this is called function scoping.
function user(){
var emp_name="John";
console.log(emp_name);
}
Output:- John
function user(){
var emp_name="John";
}
console.log(emp_name);
Output:- reference error
What is Immediately Invoke function?
When the function call itself, it is called Immediately Invoke function.
Syntax:-
(function() {
// logic
})();
Example:-
(function() {
console.log("Hello");
})();
Output:- Hello