JavaScript Functions

JavaScript functions are used to perform operations. We can call JavaScript function many times to reuse the code.

Advantage of JavaScript function

There are mainly two advantages of JavaScript functions.

  1. Code reusability: We can call a function several times so it save coding.
  2. Less coding: It makes our program compact. We don’t need to write many lines of code each time to perform a common task.

JavaScript Function Syntax

The syntax of declaring function is given below.

function functionName([arg1, arg2, ...argN])
{  
      //code to be executed  
}  

JavaScript Functions can have 0 or more arguments.

<!DOCTYPE html>
<html>
<head>
    <title>JavaScript</title>
    <script>
        function hello() {
            document.write("Hello Everybody");
        }
        function yahoo() {
            document.write("Yahoo Baba");
        }

        hello(); 
        document.write("</br>"); 
        yahoo();
        document.write("</br>");
        hello();
        document.write("</br>"); 
        hello();
    </script>
</head>
<body>
</body>
</html>