How To Write A Function in JavaScript
How To Write A Function in JavaScript
Types of Functions
There are two types of functions in JavaScript like other programming languages
such c, c++, and java etc.
o Pre-defined Functions
o User-defined Functions
Before using a function or we can say before calling a function in our program we
have to define its definition in between the curly braces. As per your requirement,
we can leave the parameter list blank as you can see in the syntax given above.
Example:
<scripttypescripttype="text/javascript">
<!--
function Hello(){
alert("Hi, there");
}
//-->
</script>
How to call the function
We can call the function when we want to use the function in the program by writing
its name as you can see below:
Hello();
Let's see a program in which, we will create a function and use it in the
program.
<html>
<head>
<title>Functions!!!</title>
<script type="text/javascript">
functionmyfirstFunction()
{
document.write("This is just a simple user-defined function.<br />");
}
myfirstFunction();
</script>
</head>
<body>
</body>
</html>
Output
To call the function somewhere else in the script, we just have to write its name as
you can see in the given example:
Example
<html>
<head>
<script type = "text/javascript">
functionsayhi() {
document.write ("Hello there!");
}
</script>
</head>
<body>
<p>Click the given button to call the function</p>
<form>
<input type = "button" onclick = "sayhi()" value = "Say Hello">
</form>
</body>
</html>
Output
functionfunctionname( parameter1,parameter2,....parameterN)
{
We can understand how to use parameters with function more easily with the help of
an example:
Program
<html>
<head>
<script type = "text/javascript">
functionsayHello(name, age,gender) {
document.write (name + " is " + age + " years old" + " and gender is " + gender);
}
</script>
</head>
<body>
<p>Click the following button to call the function</p>
<form>
<input type = "button" onclick = "sayHello(' Isabella', 23,'female')" value = "Say Hello"
>
</form>
</body>
</html>
In this program, we created a function named "sayHello ()" with three parameters:
name, age, and gender, and defined it in the head section of the HTML document. To
use this function, we also created a button using the form tag in the program's body
section and pass the values as arguments. When the user clicks that button, our
function is called and gets executed.
Output
We can understand how to use the return statement in a function with the help of an
example:
Example
<html>
<head>
<script type = "text/javascript">
functioncombinestring(string1, string2) { // function1
varcompleteString;
completeString = string1 + string2;
returncompleteString;
}
functionsecondFunction() {
var result;
result = combinestring('Java', 'Script');
document.write (result );
}
</script>
</head>
<body>
<p>Click the following button to see the function in action</p>
<form>
<input type = "button" onclick = "secondFunction()" value = "Call Function">
</form>
</body>
</html>
Function 1
Function2
To call the "secondfunction()," we have created a button for the user using the form
tag. When the user clicks on that button, our secondfucntion() will be triggered.
Note: As you can see in the program, we have used the "return" statement as the last
instruction in the body of the "completeString(string1, string2) function.
Output