Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Javascript functions explained

Thursday, August 7th, 2014 by Servage

JavaScript Function

During web and software programming, sometimes we need to write a series of computer statements, to accomplish a particular task or function. Moreover, we have to repeat these lines of code whenever and wherever we want to execute that particular function. Therefore, we pack these series of statements into a single word or phrase which identifies the unique functionality during our programming. This process is referred to as defining function, and invoking function where it is needed with that single word or phrase.

We need to define function before invoking or executing it. Defining function occurs at the beginning of programming. With web programming, we generally define function early via the <head> or <body> section of HTML. With JavaScript (JS), this is mandatory because JavaScript always executes in the sequence it is written. Invoking a function statement must never come before defining a function statement.

Declaration of Function

In JS syntax, function is packed within curly brackets {…}. When you define a function, you need to write, or declare, its name first and put the statements in these curly brackets. The definition statement of a function is not immediately executable, but it acts as a reference, and then expresses when it invokes in the code, wherever and whenever needed. For instance:

<!–DOCTYPE html>

This example calls a function which performs a calculation, and returns the result:

// <![CDATA[

function jsFunction(a, b) {

return a + b;


document.getElementById("shoaib").innerHTML = jsFunction(3, 5);

// ]]>

Roles of Functions

You can use expression in order to define a JS function so you can store these function expressions in a variable. Again, you can use a variable as a function in the following ways:

var x = function(a, b) {return a * b};

var z = x(4, 3);

In the above example, I have used an anonymous function (meaning without a proper name) because when we store functions in variables, we don’t need a function name as we have to invoke those functions using the variable name itself. Moreover, we don’t end function statements or a bunch of statement in curly brackets. Instead, we use a semicolon because here it is an executable statement, and not simple storage.

Besides these, we have used JavaScript keyword/s to define function, but we can also use JS function constructors “function()”. Therefore, our previous code will be modified a little bit in the following way:

var myFunction = function(a, b) {return a * b}

var x = myFunction(4, 3);

There are many self invoking functions used in JavaScript, but simply adding the parentheses makes it a function expression. Apart from these, JS functions can be used as “value” and “objects”. Thus, JS functions would have properties and methods, as arguments. Length property will return the number of arguments received, when we invoke the function elsewhere.

Technically, when you define function as the property of an object it becomes the method for that object. Now, when you define a function to create a new object, it automatically becomes the object constructors. For instance,

<!–DOCTYPE html>

The arguments.length property returns the number of arguments received.

// <![CDATA[

function jsFunction(a, b) {

return arguments.length;


document.getElementById("shoaib").innerHTML = jsFunction(4, 3);

// ]]>


References & More Reading
JavaScript Syntax
JavaScript Function Definitions
Learn JavaScript

Javascript functions explained, 5.0 out of 5 based on 1 rating
You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.