In JavaScript / jQuery we can define a function in several ways. We can also create a user defined function just like other jQuery functions for DOM elements. In this article I will explain some methods to declare a function and also explain how to create a user defined function.
Method 1: Basic JavaScript Function
This is a basic way to define a function in JavaScript.
Syntax
functionFunction_Name(Para1,Para2,…..)
Now we create a function that will calculate the interest for given Principal, Rate and Time.
Example
- function Interest(Principal, Rate, Time)
- {
- return (Principal * Rate * Time) / 100;
- }
- console.log("Calculated interest is= " + Interest(5000, 10, 8));

Method 2: Declare Function as Variable Name
A JavaScript function can also be defined using an expression. This function expression can be stored in a variable. After storing the expression in a variable, this variable can be used as a function.
Syntax
Varvariable_name=function(Para1,Para2,….){
//function expression
}
Now we take previous example and declare function expression in a variable.
Example
- var Interest = function (Principal, Rate, Time)
- {
- return (Principal * Rate * Time) / 100;
- }
- console.log("Calculated interest is= " + Interest(5000, 10, 8));

Method 3: Self Invoking Function
A self-invoking expression is invoked automatically, without being called. We can’t call a self-invoking function. We have to add parentheses around the function to indicate that it is a function expression. Self-Invoking function do not contain any name.
Example
- (function ()
- {
- var x = "Self-Invoking Function";
- })();
jQuery provide an excellent way to create a user defined function and use this function just like other jQuery function for DOM elements. The “jQuery.fn.extend” method is used to create user defined function. Here, jQuery.fn is just an alias for jQuery.prototype.
Syntax
uery.fn.extend({
//function implementation
return;
})
Example
- jQuery.fn.extend(
- {
- Reverse_String: function ()
- {
- var Text_ = $(this).val();
- var Split_ = Text_.split(" ");
- var Return_ = '';
- for (var count = Split_.length - 1; count >= 0; count--)
- {
- Return_ += Split_[count] + " ";
- }
- returnReturn_;
- }
- })
- $("#btnCreate").click(function ()
- {
- console.log($("#txtName").Reverse_String());
- })

Method 5: Extend Existing jQuery Function
In jQuery, we can extend any existing function by adding some extra functionality. Now we extend the functionality of “jQuery.size()” function. We know that the “size” function return the number of elements in the jQuery object.
Let us take an example.
Html Code
- <divid="div1">
- <spanid="span1">Pankaj Choudhary</span>
- </div>




Dharmendra Kumar PanditPosted Oct 5, 2019, 6:00 AM
Nice Article I'm very happy..........
Humayun Kabir MamunPosted Jan 10, 2016, 8:57 AM
Nice...
Ankit BansalPosted Jan 10, 2016, 2:47 AM
nice one..keep sharing..
Former memberPosted Jan 9, 2016, 10:14 AM
nice article
Raja TPosted Jan 9, 2016, 6:29 AM
Nice,Thanks for sharing
Santhakumar MunuswamyPosted Jan 9, 2016, 1:56 AM
Thanks for nice article. Keep it up
Manoj KallaPosted Jan 9, 2016, 1:39 AM
Good article.. Now a days I am also exploring more and more JQuery. Really helpful.