Javascript Functions

"A function is a named procedure that performs a distinct service"

A function is a small reusable subset of program that can be defined anywhere in the code and composed with set of statements.

Like other languages javascript function allow to pass value to a function, and the function can return a value.

To return a specific value other than the default value, a function must have a "return" statement that specifies the value to return.

In JavaScript, functions are objects, and can be manipulated and passed anywhere just like any other object.

The defference between function and procedures, a function always returns a value, but a procedure may or may not return any value.

Function Declarations must begin with "function" keyword.

A Javascript function hase the following general form:

Syntax

<script type="text/javascript">
function <function name> (argument list.....){
Statements .....
}
</script>

The arguments list is list of variables names. In javascript variable types is not defined in the function. Also return type is not defined for function.

Example1 : Function without parameter

<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Example of function without parameter</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function showMessage()
{
   alert('Hello Javascript');
}
</SCRIPT>
</HEAD>
<BODY>
<H3>Example of function call</H3>
<form name="myform">
<INPUT TYPE="button" VALUE="Show Message" onClick="showMessage();">
</form>
</BODY>
</HTML>
Try Now

Sometimes we pass parameters or arguments to functions, these parameters will be used inside the function for required calculations. For an example we will use two numbers to add using function.

Example2: Function with parameters

<!DOCTYPE html>
<HTML>
<HEAD>
<TITLE>Example of function parameter with return value</TITLE>
<SCRIPT LANGUAGE="JavaScript">
function add(a, b)
{
var c;
return a + b;
}

add(120, 40);
</SCRIPT>

</HEAD>
<BODY>
<H3>Example of function return value</H3>
</BODY>
</HTML>
Try Now

See Also

Javascript Built in String Object
Javascript Built in Number Object
Javascript Built in Math Object
Javascript Built in Date Object
Javascript Built in Arrays Object
Javascript Built in Boolean Object
Javascript Built in RegExp Object
Javascript Built in JSON Object


 
SHARE THIS IF YOU LIKE!
submit to reddit
SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.