How to use Function in JavaScript

In this article I am going to explain about JavaScript function.
  • 2356

JavaScript function

Function is an combination of multiple statement in single unit. After defined function its can be call when event occurs.  Event may be occurs on button click or page load. Function can also be call with in another function.

  • Function defined with function keyword with function name (function Myfunction())
  • function keyword must be in small form.
  • Function start with "{" and end with "}"
  • For calling a function simply use function name ( Myfunction() )
  • At the time of calling function function signature must be same.
  • Function can be defined inside the <head> or <body> section of the document.

How to defined a function

<html>

<head>

    <script type="text/javascript">

 

        function MyFunction() {

            alert("My first function of JavaScript");

    </script>

</head>

<body>

    <button onclick="MyFunction()">

        Click</button>

</body>

</html>

 

Function with argument

 

<html>

<head>

    <script type="text/javascript">

 

               function MyFunction(name, add) {

            alert(name);

            alert(add);

        }

        function display() {

            var a = MyFunction("Shayam", "Delhi");

 

        }

 

    </script>

</head>

<body>

    <button onclick="display()">

        Click</button>

</body>

</html>

 

Output

funarug.jpg

 

Return type function

 

<html>

<head>

    <script type="text/javascript">

 

               function MyFunction(name, add) {

            alert(name);

            alert(add);

        }

        function display() {

            var a = MyFunction("Shayam", "Delhi");

 

        }

 

    </script>

</head>

<body>

    <button onclick="display()">

        Click</button>

</body>

</html>

 

Output

funret.jpg

Further Readings

You may also want to read these related articles: here
Ask Your Question 
 
Got a programming related question? You may want to post your question here
 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.