Whenever we want to perform some specific operations we can use functions, because of the reusablity of these functions.
Here are the advantages of the Functions.
๐.๐๐จ๐๐ ๐๐๐ฎ๐ฌ๐๐๐ฅ๐ข๐ญ๐ฒ
We can call same function many time to reuse it.
๐. ๐๐๐ฌ๐ฌ ๐๐จ๐๐
Becuase of code reusablity line of code get minimum, so you have to write less code.
๐๐ฒ๐ง๐ญ๐๐ฑ ๐จ๐ ๐ญ๐ก๐ ๐๐ฎ๐ง๐๐ญ๐ข๐จ๐ง
function functionName([arg1,arg2,...argN]){
//Code to be executed
}
๐๐ฑ๐๐ฆ๐ฉ๐ฅ๐
function sayHello(){
console.log("Hello JavaScript");
}
sayHello();
๐ ๐ฎ๐ง๐๐ญ๐ข๐จ๐ง ๐ฐ๐ข๐ญ๐ก ๐๐ซ๐ ๐ฎ๐ฆ๐๐ง๐ญ๐ฌ
Function can be called through passing the arguments.following is the example of function with arguments.
function addition(num){
let value = num + num;
console.log(value);
}
addition(4);
๐ ๐ฎ๐ง๐๐ญ๐ข๐จ๐ง ๐ฐ๐ข๐ญ๐ก ๐ซ๐๐ญ๐ฎ๐ซ๐ง ๐ฏ๐๐ฅ๐ฎ๐
Whenever we call a function it returns the value which can be used in the program.
function square(num){
let value = num*num;
return value;
}
console.log(square(4));
๐ ๐ฎ๐ง๐๐ญ๐ข๐จ๐ง ๐ข๐ง ๐ฏ๐๐ซ๐ข๐๐๐ฅ๐
function can be assigned to the variable.following is the example of it.
var square = function(num){
let value = num*num;
return value;
}
console.log(square(4));
๐๐ซ๐ซ๐จ๐ฐ ๐๐ฎ๐ง๐๐ญ๐ข๐จ๐ง
Arrow function were introduced after ES6,basic example of arrow function is given bellow.
var sayHello = ()=>{
console.log("Say Hello");
}
sayHello();