Google News
logo
Javascript Functions
Functions are very important and useful in any programming language. We can use code reusability function several times so it save coding. If you have a few lines of code that needs to be used several times, you can create a function including the repeating lines of code and then call the function wherever you want. 
 
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().
Function Syntax :
function sample_name(parameters)
{
statements
}
Example :
<html>
<head>
<title>Function Example</title>
</head>
<body>
 
 
<script type="text/javascript">
  function web()
  {
alert("www.freetimelearning.com");
//document.write("www.freetimelearning.com")
  }
</script> 
 
<input type="button" onclick="web()" value="Click Here"/>
 
</body>
</html>
Output :
Function Parameters :
A function can have one or more parameters, which will be supplied by the calling code and can be used inside a function. JavaScript is a dynamic type scripting language, so a function parameter can have value of any data type.
<html>
<head>
<title>Function Example</title>
 
<script type="text/javascript">
 function book(name, year)
 {
 document.write (name + year + " Turorial.");
 }
</script>
     
</head>

<body>

  <form>
     <input type="button" onclick="book('JavaScript ', 2017)" value="Click Here">
  </form>

</body>
</html>
Output :
Function Arguments Object :
Using the arguments object, you can call a function with more arguments and it is useful if you don't know in advance how many arguments will be passed to the function. The individual arguments contained in the arguments object can be accessed in the same way array elements are accessed.
<html>
<head>
<title>Function Argument Objects</title>
</head>
<body>
 
<script type="text/javascript">

  function website(first, mid, last) {
  alert("Welcome to " + arguments[0] + "" + arguments[1] + "" + arguments[2]);
  }
 
  website("www.", "freetimelearning", ".com"); 
 
  website("www.", "freetimelearn", ".com"); 

</script>

</body>
</html>
Output :
Function with Return Statement :
The return statement is required if you want to return a value from a function. This statement should be the last statement in a function.
<html>
<head>
<title>Function with Return Value</title>
</head>
<script type="text/javascript"> 
 
  function website(){  
  return "www.freetimelearning.com";  
  }  

</script> 
 
<body>
 
<script type="text/javascript">
  document.write(website());  
</script>
 
</body>
</html>
Output :

www.freetimelearning.com

Another Example :
<html>
<head>
<title>Function with Return Value</title>
<script type="text/javascript">
function sample_name(first, mid, last)
{
var website;
website = first + mid + last;
return website;
}
 
function my_website()
{
var result;
result = sample_name('www.', 'freetimelearning', '.com');
document.write (result );
}
</script>
 
</head>
 
<body>
 
<form>
  <input type="button" onclick="my_website()" value="Click Here">
</form>
 
 
</body>
</html>
Output :