JavaScript – A Different Beast, Part 3: Functions

Function Parameters

In JavaScript parameters are passed by value or by reference based on the type of the parameter. Programmers do not have any control over this behaviour. Primitives are always passed by value and objects are always passed by reference. Code-2.3 shows an example of pass by value and code-2.4 shows an example of pass by reference.

//Code – 2.3:  Pass by value
//I have ten bottles
var greenBottles = 10;
console.log(greenBottles); //prints  10

function accidentalFall(count) {
      count = count - 1; // Remove one

//oops. accident!
accidentalFall(greenBottles); // Pass by value
console.log(greenBottles); // still have 10 bottles!!

In code-2.3, the value of ‘greenBottles’ remain the same even after the function call which changes the value. This is because the parameter is passed by value, and the function accidentFall has its own copy of the variable which is getting decremented leaving the original passed value the same.

//Code – 2.4:  Pass by reference
//arrays are objects in JavaScript
var arr = [1,2,3,4,5];
console.log(arr); //prints [1,2,3,4,5]

function change(x){

console.log(arr); //prints [5,4,3,2,1]

In code-2.4, the call to reverse changes the original array itself, since the variable x inside the function change refers to the same object that is passed to it.

Variable Arguments

JavaScript has a special arguments object available within all functions which holds all the arguments passed to the function. Code-2.6 shows how this works.

//Code – 2.5:  The arguments object
function f1(arg1,arg2){
    console.log('f1 says: ',arg1,arg2);
    console.log('f1 says: ',arguments);

f1(1,2); //f1 says 1 2, f1 says 1 2
f1(10,20,30); //f1 says 10 20, f1 says 10 20 30

function f2(){
   console.log('f2 says: ',arguments);

f2(10,20,30); //f2 says 10 20 30

From this example, we can see that the arguments object always holds all the parameters passed to any function. This can be used to implement variadic functions or ellipsis function feature seen in many other programming languages.  I have found this to be very useful in simulating function overloading mechanism when developing toolkit APIs.

Next topic in this series: Scopes and Scope Chain


About Rahul Mohan

Technical researcher interested in model-driven development of enterprise business applications. Passionate about web technologies and a huge admirer of open-source especially in the Java landscape. When I am not doing any of the above you can find me with my camera ( or listening to music. (In fact, the title of the blog is inspired by Led Zeppelin's Physical Graffiti. )

Posted on May 12, 2011, in JavaScript, Tutorial and tagged , , , , , . Bookmark the permalink. 7 Comments.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: