Loading

Functions

  • A function is a block of code used to perform a specific task.

Syntax:
function functionName(parameters) {
 // Code to be executed
 // This is the function's body
}
  • function: This keyword is used to declare a function.
  • functionName: This is the name of the function. Choose a descriptive and meaningful name for your function, following JavaScript naming conventions.
  • parameters: These are optional. You can pass data into a function through parameters. Parameters act as variables within the function's scope, and you can use them in the function's body.
  • {}: The curly braces enclose the function body. This is where you put the code that the function will execute.

Example:

<html>

<body>
   <script>
      function msg() {
         alert("hello! this is message");
      }  
   </script>
   <input type="button" onclick="msg()" value="call function" />
</body>

</html>

Output: