codedamn logo
codedamn

Type of function in JavaScript

  • Vishnupriya's profile image
    Vishnupriya
    Team codedamn
Type of function in JavaScript

Different types of functions in JS, that everyone should know!

JavaScript Functions

JavaScript provides capabilities similar to most of the scripting and programming languages.

In JavaScript, a feature permits you to outline a block of code, supply it a name, and then execute it as usually as you want.

A JavaScript function may be described the usage of function keyword.

//The syntax for defining a function


function <name-of-function>()
{
    // code to be executed
};

//calling a function
<name-of-function>()
//Example
function ShowMessage()
 { alert("Hey everyone!"); 
}
 ShowMessage();

We have defined a function in the above example. Here the function is ShowMessage which can be executed by using operator ().

function may have one or extra parameters, with the intention to be supplied by using the calling code and may be used interior a characteristic. JavaScript is a dynamic type scripting language, so a function parameter could havevalue of any statistics kind.

//example
function ShowMessage(firstName, lastName) {
    alert("Hey " + firstName + " " + lastName);
}

ShowMessage("Amit", "Singhania", "Mr."); // display Hey Amit Singhania
ShowMessage("Andrew"); // display Hey Andrew undefined
ShowMessage(); // display Hey undefined undefined

By default functions in JS can use arguments objects. An argument’s object includes the cost of every parameter.
The arguments object is an array-like itemyou could access its values using an index similar to an array. but, it does no longer guide array methods.

//example
function ShowMessage(firstName, lastName) {
    alert("Hey " + arguments[0] + " " + arguments[1]);
}

ShowMessage("Amit", "Singhania"); 

ShowMessage("Andrew", "Hills");

ShowMessage(100, 200);

Want to learn Solidity? Click here!
 For the Advance Node.js course, check here!
Anonymous Function

Without any name, we can define a function in JS. We can call this unnamed function an anonymous function. This function should be given to a variable.

//example
var showMessage = function (){
 alert("Hey everyone!"); 
}; showMessage();
 var sayHey = function (firstName) { 
alert("Hey " + firstName); 
}; 
showMessage();
 sayHey("Andrew");
//example
function addOrEven(number) {
if (number%2 == 0) {
return "Number is even"
} else {
return "Number is odd"
}
}

 

Nested Functions

In JS, a feature may have one or more inner features. Those nested capabilities are inside the scope of the outer function. The inner feature can get admission to variables and parameters of the outer featurebut, the outer feature cannot get admission to variables that describe dinner capabilities.

//example
function ShowMessage(firstName)
{
    function SayHey() {
        alert("Hey " + firstName);
    }

    return SayHey();
}

ShowMessage("Andrew");

Named Function

Named feature is the feature that we outline within the code after which name it every time we want it by using referencing its name and passing a few arguments to it. Named capabilities are beneficial if we need to name a characteristic common to skip exceptional values to it or run it in numerous instances.

//example

function addOrEven(number) {
if (number%2 == 0) {
return "Number is even"
} else {
return "Number is odd"
}
}

Invoked function

Invoked feature expression runs as soon as the browser encounters it. The benefit of this characteristic is that it runs straight away in which it’s positioned in the code and produces a direct output. which means it is unaffected by means of code which seems in addition down inside the script which may be beneficial.

Functions execute when the character is referred to as. This procedure is referred to as invocation. you can invoke a function with the aid of referencing the function nameobserved with the aid of an open and closed parenthesis: ().

An invoked function expression is brilliant for speedy populating a variable or argument in a larger characteristic or belongings in an object and is often hooked to occasion listeners for instant output.

// example
let message = (function () {
  let name = "Andrew Hills";
  return name;
})();
//Immediate ouput gets created:
result;  //"Andrew Hills"

Function Return
When JS reaches a go-back declaration, the function will forestall executing.
If the characteristic becomes invoked from a statement, JS will “return” to execute the code after the invoking statement.
Capabilities often compute a return cost. The go-back price is “returned” returned to the “caller”.

let x = myFunction(6, 2);   // Function is called, return value will end up in x

function myFunction(a, b) {
  return a * b;             // Function gives the product of a and b
}

Assigning a characteristic to a variable allows us to skip this variable as a parameter to some other functionthis is specifically useful in scenarios that require runtime flexibility. you’ll particularly use such functions to run a load of code in response to an event firing, for instance, a button being clicked the use of an occasion handler.

whilst a function accepts another function as a parameter or returns a characteristicit’s far called a better-order function. You’ve in all likelihood already used a group of better order functions and don’t even realize it: Array. prototype.map and Array.prototype.filter is higher-order capabilities

A function is a subprogram made to perform a selected mission.
feature definitions are hoisted — expressions are not.
functions are executed whilst they are known as. This is known as invoking a feature.
Values can be handed into features and used inside the function. The name of the price is referred to as a parameter. The actual price itself is known as an issue.
features always go back a value. In JavaScript, if no go-back value is designated, the feature will return undefined by means of default.
Thank you!!

Read our blog on “Sort in JavaScript- How and when to use it?

User avatar