JavaScript Function

function keyword to define JavaScript function. In JavaScript or any programming, function is a relatively codes (bunch of reusable codes) that calls anywhere in your programs.

A function can derive and return a value to that called program.

This way you could ignore to write the same code every time and create a modular structured programs.

You can derive your program into a number of functions, this way you manage your program very easily.

In this lesson you learn the basic function syntax, function definition, how to call empty function and arguments function, and finally access returns function value.

Function Syntax

JavaScript function declare by following syntax,

function functionName(arguments) {
    statements
}

Arguments

  • function: function, is JavaScript reserved keyword
  • functionName: Unique function name that is unique within programs.
  • arguments: Optional, list of arguments separated by comma.

Simple JavaScript Function

Before calling function, we need to declare function. After declare function, when you calling that function derive only that time.

function msgfun(){ 
    document.writeln("hello world!"); 
}

msgfun();

Run it...   »

JavaScript Function Pass Arguments

In this example, name and salary are variables they are passed as an argument to empfun() function. Function body takes two arguments and one optional argument.

Optional arguments allowance indicates if you aren't passing third argument, then default third argument takes assignment value 1300.

function empfun(name, salary, allowance=1300){ 
    document.writeln("Employee name: " + name);
    document.writeln("Employee Salary: " + salary);
    document.writeln("Employee Allowance: " + allowance);
}
   
var name = "Opal Kole";
var salary = 15000;

empfun(name, salary);

Run it...   »

Browser Compatibility (optional arguments)

  • Mozilla Firefox 15+

And if you are passing three arguments, then use that passing value to derive this function.

var name = "Opal Kole";
var salary = 15000;
var allowance = 1500;

empfun(name, salary, allowance);

Run it...   »

JavaScript No Arguments Function

You can write a function without passing any arguments. Function can derive itself with depend internal defined values.

function empfun(){ 
    var name = "Opal Kole";
    var salary = 15000;
    document.writeln("Employee name: " + name);
    document.writeln("Employee Salary: " + salary);
}

empfun();

Run it...   »

JavaScript Function Returning a Value

You can use JavaScript return statements to return a value. Following example, calculate area of a circle.

function cal(radius ){
    var area;
    area = 3.14 * radius * radius;
    return area;
}

var radius = 2.0, circle_area;
circle_area = cal(2.0);

document.writeln("Radius of Circle : " + radius);
document.writeln("Area of Circle : " + circle_area);

Run it...   »

JavaScript Nested function

When you specify a function within another function, child function can access parents function scope. You can call the child function outside of parent function by following way,

function cal(){
    var PI = 3.14;
    function area(radius){
        var area = PI * radius * radius;
        document.writeln("Area of Circle: " + area);
    }
    cal.area = area;
}

var radius = 2.0;
cal();
cal.area(radius);

Run it...   »

Browser Compatibility

  • Google Chrome
  • Mozilla Firefox
  • Internet Explorer
  • Opera
  • Safari

Note: Here details of browser compatibility with version number may be this is bug and not supported. But recommended to always use latest Web browser.