codescracker
javascript

JavaScript Function with Parameters or Arguments



« Previous Tutorial Next Tutorial »

A function with parameters or arguments accepts some values when it is called. Basically arguments are the values that you pass to a function, which has corresponding parameters to store them.

Note - Arguments are passed while calling a function.

JavaScript Function Parameters/Arguments Example

Here is an example demonstrates function arguments/parameters in JavaScript:

<!DOCTYPE HTML>
<html>
<head>
   <title>JavaScript Function Parameters/Arguments</title>
   <script type="text/javascript">
      function myfun(val)
      {
         alert(val);
      }
   </script>
</head>
<body>

<h3>JavaScript Function Parameters/Arguments Example</h3>
<form>
   <input type="button" value="Call Function"
   onclick="myfun('Welcome to JavaScript function with parameter/argument.')">
</form>

</body>
</html>

Here is the sample output of the above JavaScript function with parameter/argument example. This is initial output:

javascript function parameters

This is the output produced after pressing Call Function button:

javascript function arguments

Here is the live demo output produced by the above JavaScript functions with parameters or arguments example program.

Let's take one more example on function arguments/parameters in JavaScript:

<!DOCTYPE HTML>
<html>
<head>
   <title>JavaScript Function Parameters/Arguments</title>
</head>
<body>

<h3>JavaScript Function Parameters/Arguments Example</h3>
<script type="text/javascript">
   function calsum(val1, val2, val3)
   {
      var sum = val1 + val2 + val3;
      document.write("Total = " + sum);
   }
   document.write("Here the sum is calculated by passing arguments to the function named calsum<br/>");
   calsum(10, 20, 30);
</script>

</body>
</html>

Here is the sample output of the above JavaScript function arguments/parameters example:

javascript function parameters arguments example

Following is the live demo output of the above functions with parameters example in JavaScript.

JavaScript Online Test


« Previous Tutorial Next Tutorial »



© Copyright 2021. All Rights Reserved.

CodesCracker