spacer
Yehuda Shiran February 10, 2001
Passing Parameters in JavaScript
Tips: February 2001

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
ActiveState Debuts Open Source Business Suite
Salesforce Offers Visual App Builder
Codesion Steps Out From CVS's Shadow

Some C++ programmers expect JavaScript to support passing parameters by reference. When we pass a parameter by reference, we pass the address of the parameter instead of its value. Once inside the function, we can change the stored value in this address. If JavaScript would have supported passing by reference, the following script would have resulted in an alert box showing the value of 6 (3 is passed to the function change() where it is doubled):

<SCRIPT LANGUAGE="JavaScript">
var a = 3;
change(a);
alert(a);

function change(value) {
  value = value * 2;
}

</SCRIPT>
But JavaScript does not support passing by reference. It passes its parameters by value only. A function does not have any way to know the address of its parameters, and hence cannot change the stored parameter value. The script above generates an alert box with the value of 3, as the variable a is not being changed by the function change().


People who read this tip also read these tips:

Look for similar tips by subject:


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

webref The latest from WebReference.com Browse >
Use Web Caching to Make Your Web Site Faster · Creating an Online Shopping Cart Mechanism in PHP · Log JavaScript Errors Using an AJAX-driven Web Service
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Configuring Granular Settings for a Database Level Audit · The Perils of a Web 2.0 Transition on Your Business Processes · Facebook Redesigns Site —Again — Nears 400M Mark