Learn JavaScript

JavaScript Functions

JavaScript Functions is nothing, but a block of code snippet. It can be called from anywhere in the JavaScript program to execute the task for which, it is meant to design. The block of code snippet have the nature of reusability. With functions we eliminate the possibility of writing the same piece of code time and again. It gives liberty to programmers to write the modular programs, that means programmers can write a comparatively big program into some small and easily manageable chink of codes.

You probably have noticed that, we kept using the write() function to print something every time we write a program. We have used write() function many time, but never write the code this function, why; because the write() function is written within the JavaScript, and whenever the need arises, we just call this function, by following proper syntax.

JavaScript allows us to write our own functions to perform some specific tasks. Here we will be learning every possible aspects of JavaScript Functions.

Standard Syntax of JavaScript Functions

function function_name(function_parameters_list)
{
Set of statements to be executed;
return;
}

Note: Each and every component of a function, which is required in order for the execution of the function successfully, is discussed below:

JavaScript Functions Definition

Before using a function in a program, we need to first define the function with appropriate syntax, shown in above example. The explanation for every component of the definition of a function is listed below:

  • function: A keyword used to define a function.
  • function_name: Name of the function, defined uniquely.
  • function_parameters_list: Set of parameters, passed as the argument of the function. This list can be empty.
  • Set of statements: A code snippet, that is written to perform some task.
  • return: As the control reaches to return, it stops the execution of the function and return a value.

JavaScript Calling a Function

In order to call a function from anywhere in the program you just need to write the name of the function with or without the function_parameters_list, accordingly.

JavaScript Functions Parameters

JavaScript allows us to pass some parameters, while calling a function. The scope of those parameters remain within the function body and all manipulation is done around these parameters. To declare more than one parameters use comma separator to separate these parameters.


Example: Understanding JavaScript Functions without Parameters.

<html>
<body>
<h2>Most Powerful Characters of Marvel</h2>
<script>
function juf(){
var pcm = ["CaptainAmerica", "IronMan", "Hulk", "CaptainMarvel", "WandaMaximoff"];
var w = "";
var i;
for (i = 0; i < 5; i++)
{
w += pcm[i] + "<br>";
}
document.write(w);
}
juf();
</script>
</body>
</html>

Output

Most Powerful Characters of Marvel

CaptainAmerica
IronMan
Hulk
CaptainMarvel
WandaMaximoff


Example: Understanding JavaScript Functions with Parameters and return.

<html>
<body>
<h2>JavaScript Functions with parameters and return</h2>
<script>
var v = juf(25, 37);
document.write("Summation of 25 and 37 is" + " " + v);
function juf(x, y)
{
return x + y;
}
</script>
</body>
</html>

Output

JavaScript Functions with parameters and return

Summation of 25 and 37 is 62


Audience

This JavaScript tutorial is designed to help the beginners as well as the professional developers. For better understanding the language of the tutorial is kept simple and easy to understand.

Prerequisites

This tutorial demands to have the basic knowledge of the programming languages such as c or c++, HTML, loops for the better understanding of the JavaScript programming language. The other basic requirement to learn JavaScript through this tutorial is the basic knowledge of computer and information technology so you can easily acquire a better understanding JavaScript programming.

Community Helping

In case you find any kind of error like typing error, spelling mistakes, incorrect grammar, incorrect information or any other mistakes. Please feel free to write to us at: akyadav@etutororg.com.