Javascript Functions

Javascript Functions

Functions are the block of code that we want to use again and again simply by calling with their name.


You can make your own functions and use them whenever and wherever you want just simple by making a function and calling that function.

Syntax of Creating a Function

function functionName() 
{
    Your code here;
}

//For calling your function
functionName();

Example of Function

function demo()
{
  alert( "Hello Javascript" );
}
demo();

This will display Hello Javascript.




Syntax of Function with parameters

function functionName(parameters) 
{
    your code here
}

/*For calling your function*/
functionName(parameter);

You can send as many parameters in function as you want.


Example of Function with parameters

function demo(val)
{
  alert( "Here is your value is "+val);
}

demo(10);

This will display Here is your value 10.




Example of Function Return Value

function demo(val)
{
  var add=val+10;
  return add;
}

var val2 = demo(10);
alert( "Here is your value "+val2 );

This will display Here is your value 20.

PreviousNext

Subscribe Our Newsletter And Get Tutorials
And Offers Via Email

Subscribe Our Newsletter And Get Tutorials And Offers Via Email