Search within TutorialsPark

Javascript Functions

Functions are a set of Javascript statements that perform a specific task.

Javascript Functions

Javascript functions are a collection of statements, either named or unnamed(anonymous) that can be called when required within a Javascript program.

Functions can accept arguments, which are used as inputs for the function.The inputs are acted upon and the results are returned to the parent function via return value.

Functions are used when some task needs to be done repeatedly within a program. Rather than writing the same code over and over, you can use a function and call it when required to perform the task.

A good example is a CellPhone, you can press a button to make a call, use an app to play a game, listen to music. You care for results and not how they are obtained, like a blackbox.

Javascript: Defining Function

A function is defined by using the keyword function followed by the name of the function and a set of parentheses to hold parameters or values to be recieved by the function.

The function can be called when required by using the function name followed by the argument enclosed within parentheses(seperated by comma, in case of multiple arguments) .

Syntax: Javascript functions

Example: Javascript Functions

Give it a TRY! » Note: To check whether a funtion has been defined use typeof() operator. Eg : typeof(function_name)

Javascript Functions: Passing Arguments

Arguments can be passed to the function by enclosing the values in a parentheses after the function name and sent as a comma separated list of arguments while calling the function.

The arguments recieved as parameters by the recieving function and are stored in local variables, but as soon as the function ends the variable disappears

Syntax: Javascript Function passing arguments

Example: Javascript Functions - Passing Arguments

Give it a TRY! » Note: There is no limit to the number of arguments that can be passed to a function.

Javascript Function Call using a link

You can call a function directly from a hyperlink, by making use of Javascript pseudo protocol Javascript: instead of a URL within href

Its done by using the href attribute and placing the Javascript: protocol and function call within the quotes instead of URL.

Syntax: Javascript function call using a link

Example: Javascript Function Call Using a link

Give it a TRY! » Note: When the link is clicked instead of visiting the URL the function is called.

Javascript Function Call using Events

Javascript Functions can also be invoked by events which are triggered by some user action like clicking of a button, moving mouse over a link etc.

Javascript events are handled by event handlers, when the event is called the function is called.

Syntax: Javascript Function call using Events

Example: Javascript Function Call using Events

Give it a TRY! »

Javascript Function : Scope of Variables

The scope of the variables defines where the variable can be used in the program(i.e its visibility) .

The variables declared outside the function have a global scope, i.e they can be used and manipulated any where in the program.

The variables declared using the keyword var within a function has a local scope, i.e they can be used and manipulated within the function only.

Example: Javascript Function - Scope of Variables

Give it a TRY! » Note: The local variables are not visible outside the function or their origin.