Advanced Java and Web Technologies for JNTUK
Blog providing beginner tutorials on different web technologies like HTML, CSS, Javascript, PHP, MYSQL, XML, Java Beans, Servlets, JSP and AJAX

23/02/2014 Categories: Javascript. No Comments on Functions in Javascript

A function is a block of code which can be executed again and again. Functions make the code modular which improves maintenance. Functions in JavaScript are similar to functions in C language. A function definition is as shown below:

The function definition consists of function header and the body of the function (a compound statement). The function header consists of function keyword, function name followed by parameters (if any) enclosed in round brackets. The function body consists a set of statements and an optional return statement that are executed when the function is called. A function call is as shown below:

functionName(param1, param2, … , paramN); 

In JavaScript functions are objects. So, variables that reference them can be treated as object references. Example for function definition and function call is given below:

In JavaScript, a function definition must occur before a function call. So, it is wise to define a function in the head section of the page.


Local Variables

The scope of a variable denotes the range of statements over which the variable is visible. In JavaScript we have two kinds of variables: global variables and local variables.

Variables that are declared implicitly (without var) outside a function or inside a function definition have global scope. Variables that are declared explicitly (with var) outside (including compound statements) a function also have global scope. Variables that are declared explicitly inside a function definition have local scope.

When a global variable and a local variable have the same name, the local variable hides the global variable.



The parameters available in the function call are known as actual parameters while the parameters in the function definition are known as formal parameters. When the formal parameters are more than the actual parameters, remaining formal parameters are set to undefined. When the formal parameters are less than the actual parameters, remaining actual parameters are truncated.

The actual parameters passed to a function can be accessed through the property array, arguments. So, even when the formal parameters are less than the actual parameters, all the actual parameters are directly accessible through arguments property.

We can use the arguments property as shown below:

arguments.length  //Gives the number of actual parameters

arguments[n]  //Access the nth actual parameter 

In JavaScript, parameters are passed by value. Even though object references are being passed, references itself are passed as a value.

Suryateja Pericherla

Suryateja Pericherla

Hello, I am Suryateja Pericherla working as an Asst. Professor in CSE department at Vishnu Institute of Technology. I write articles to share my knowledge and make people knowledgeable regarding certain topics.
Suryateja Pericherla

Latest posts by Suryateja Pericherla (see all)

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Scroll Up
Thank you for visiting! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: