In my previous article we learned about the basics of JavaScript. In this article, we will take a look at how functions are declared and worked in JavaScript.
Introduction
A function is nothing but a block of reusable code. Like a variable, the function can also be defined anywhere in your JavaScript code section. JavaScript function allows us to divide a big programming code into small and manageable program code, known as modular development. We can create single or multiple functions in JavaScript.
In JavaScript, like other advanced programming language e.g. C++, C# etc., we can also define our own JavaScript function. We can call these functions for execution whenever we need them. So function makes our code easily reusable and maintainable within our script.
Function Declaration
A function in JavaScript is declare using function keyword. A function keyword is a reserve keyword of JavaScript. In the below example I created a sample function and called it for execution. For example,
- function print()
- {
- document.write(“Learning JavaScript.”);
- }
- // executes print function
- print();
In the above example, the function declaration started with the function keyword followed by a function nameprint. In JavaScript function within parenthesis {} (body section) we can write our code for execution, it means what operation we want from this function.
Function with Argument
In JavaScript we can also create argument based function. These are variables, either numbers or strings, with which the function is supposed to do something and the output of the function depends on the arguments you pass in it. We can use as many arguments according to our requirements. e.g.
- function example(a,b) {
- document.write(a+b);
- }
- Example(“Learning”,” JavaScript”);
The example above declares a function named example with two arguments (a, b) and calls it. The output of the above example will be based on the given argument. e.g. “Learning JavaScript”.
Returning a value
In JavaScript, like other programming languages, we can also return value from a function. If we want to return any value from function then we need to use return keyword followed by value what we want to return. The return is a reserved keyword by JavaScript. e.g.
- function sum(a, b)
- {
- //return value using return keyword
- return a + b;
- }
- //function whatever return, It will store into result variable.
- var result = sum(2, 5);
- //print this variable.
- document.write(result);
In the above example we declare a function named sum with comma-separated two-argument (a, b) and call it for execution. The output of the above example will be based on the given argument. e.g. 7.
If a function does not return anything, its result will be a special value, known as undefined. e.g.
- function nothingToReturn()
- {
- //nothing to be return
- }
- //try to get value from function
- var result = nothingToReturn();
- //print the value of result. It will be undefined.
- document.write(result);

SubashPosted Aug 30, 2016, 12:43 AM
Useful tip for me
Rahul Pushpendu BhaskarPosted Jun 30, 2016, 6:46 AM
Nice Article..Thank you
Satyam LimbaniPosted Mar 8, 2016, 2:01 AM
Again good article by you
Debasis SahaPosted Mar 7, 2016, 11:53 PM
Nice..
Asfend YarPosted Mar 7, 2016, 12:05 PM
nice
kalu singh raoPosted Mar 7, 2016, 10:28 AM
Nice...
Guna PokhrelPosted Mar 7, 2016, 10:13 AM
Learning a lot about javascript
Sibeesh VenuPosted Mar 7, 2016, 7:29 AM
Nice Share
Vignesh ManiPosted Mar 7, 2016, 6:06 AM
Good
Mohammed IbrahimPosted Mar 7, 2016, 4:11 AM
nice
Sonu ChaudharyPosted Mar 7, 2016, 1:16 AM
nice one