JavaScript Functions

A JavaScript function is executed when we make call to it. In short, we can say that JavaScript function is a block of code designed to perform a particular task. You can call function using some event or you can also nest function and call it with in the body of another function.

JavaScript functions are identified with the function keyword, followed by the name, followed by parentheses (). This is the complete Syntex of JavaScript function.

Function names can contain letters, numbers, underscores, and dollar signs and variables name will also follow the same naming conventions.

function name(parameter1, parameter2) {
  // whole java script code to be executed
}

JavaScript Function Invocation:

The code inside the function will execute in following scenarios:

  • When an event occurs (when a user clicks a button)
  • When it is invoked (called) from JavaScript code
  • Automatically (self-invoked function)

Types of JavaScript Functions:

Parametrized Function:

Following function is Parametrized function and it will accept two values and then it returns the sum of numbers (only if input numbers are integers). What if you will pass string in it then it will return concatenated data.

function AddNumbers(a1, a2) {
  return a1 + a2;
}

Parameter less Function:

This will be a normal function which will perform some tasks and it will not have any input parameters.

function ShowUsers() {
  return "Welcome to code config";
}

Functions with Return:

When JavaScript encounters a return statement, the function stops executing. If a function is called from a statement, JavaScript will “fall back” to execute the code after the calling statement. Functions often evaluate return values. The return value is “returned” to the “caller” function.

// When this Function is called, it will return value will end up in x parm
let x = MultiplyNumbers(2, 5);

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

Self Invoked functions:

A self-invoked function is an unnamed (anonymous) function that is called immediately after its definition. An anonymous function surrounded by one set of parentheses, followed by another set of parentheses (), performs the execution.

Where to Use Self Invoked functions:


Self-invoking functions are useful for initialization tasks. For example, if we have a website where we want to attach event handlers to DOM elements and other initialization work, then self-invoking functions will be the best tool for this same.

(function(){
    console.log("This function is called immediately");
})();

You can also pass parameters to the self-invoking functions. It is a commonly used practice to pass references to global objects.

The self-invoking functions can have variables and methods, but they are not accessible from the outside. To access it, you must pass the global window object as a parameter.

(function (a, b, c) {
  // body
}(window, document, jQuery));

I hope you have enjoyed reading above article. You can check more on JavaScript HERE.

Leave a Reply

Your email address will not be published. Required fields are marked *