javaScript functions

javascript functions can be used to create script fragments that can be used over and over again. a javascript function are used to encapsulate code that will be executed when 'someone' calls it

look this code

    <!doctype  html>
                       function   showinformation(){
                          <h1>user information </h1>
                          <button  onclick="showinformation()">show</button>


javascript functions are declared with the function keyword, operation code inside curly {} braces.

    function         functionname()
                                                 // function code block

    function         functionname()
                                            {   code part 
                                                 return  statement;   //this is optional statement

a javascript function is executed when a call to that function is made anywhere within the script, the page, an external page, or by an event.

functions are always guaranteed to return some value when executed.

note:forward references to functions are generally not allowed; in other word, you should always define a function before calling it.

javaScript functions with arguments

you pass information to a function by enclosing the information in parentheses after the name of the function.pieces of information that are passed to a function are called arguments or parameters.

calling method syntax:


when you declare the function, declare the argument

     function        showinformation(var1,var2,var3){  code part  }

the variables and the arguments must be in the expected order.the first variable is given the value of the first passed argument etc:

                       function   showinformation(name,lastname,age,salary){
<button  onclick="showinformation('vinod','kumar',25,100000)">show

javaScript function with return statement

when using the return statement, the function will stop executing, and return the specified value.

                       function   sum(a,b,c){
                                                      return  (a+b+c);
<button  onclick="alert('sum ='+sum(20,40,50))">show sum</button>

recursive functions

a javascript recursive function is one that calls itself.

    function  factorial(n){  if( n==0 ){ return 1 ; } else{ return n * factorial( n-1 ); } }

local variables

a variable declared within a javascript function becomes local and can only be accessed from within that function.(the variable has local scope).

local variable is one limited to the particular block of code it is defined within.

global variables

variables declared outside a function, become global, and all scripts and functions on the web page can access it.

the scope of javascript variables

the lifetime javascript variables starts when they are declared.

global variables are removed from memory when you close the page.

local variables are removed from memory when the function execution is completed.

      var     value  = 30;
         function   show(){
                                   var   name ="variable scope";
        var   top  = "this is javascript local scope variable";//define a local variable.   


the javascript functions can be constructed in three main ways.we begin with three 'hello, world!' examples:


function      hello()
   	  alert("hello, world!");

 var  hello = function()
       alert("hello, world!");
     anonymous function

var hello = function(      
  'alert("hello, world!");' 
       function as object

each function:

  • can be called with hello()
  • does not expect any arguments
  • performs an action to alert the user with a message
  • undefined is returned when execution is finished