Functions in JavaScript

Functions in JavaScript

ยท

1 min read

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();
ย