homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

Function with arguments: why is this argument necessary?
A question about JavaScript basics.

 7:28 am on Apr 9, 2009 (gmt 0)

Hi. I am new to JavaScript and following th W3 Schools documentation on it. I came across an example I just cannot understand.
What's confusing me is the "txt" part:

function myfunction(txt)

Why can't it display the alert without the "txt" argument?
Am I overlooking something here?

Also, it seems like you can achieve the same result more efficiently with this method:
So please, enlighten me on what role the argument plays and how it's useful.



 8:03 am on Apr 9, 2009 (gmt 0)

the txt part (argument) allows the function to be reused



 12:57 pm on Apr 9, 2009 (gmt 0)

Welcome to WebmasterWorld! As daveVK pointed out, one thing that we strive for when developing code is "reuse". The simple example that you have above is not really a good "real world" case, but I think it's simply meant to show how to pass values to functions. You might have instead wanted to alert the text of the button that was pressed. So you could have done:

function myfunction(txt)
alert("You clicked " + txt);

<input type="button"
<input type="button"

If you had simply hard coded a value to be alerted, then your function is not really re-usable.


 5:50 pm on Apr 9, 2009 (gmt 0)

Thank you both for your replies.

I suppose what confuses me the most is that "txt" never shows up anywhere. I would understand if the button displayed "txt" when you click it, but it does not. It displays "Hello".

Unless you are saying that all messages like "Hello" are sub-sets of "txt". It just seems to me like I should be able to delete "txt" altogether and still have it work - but that is not the case.


 5:57 pm on Apr 9, 2009 (gmt 0)

txt is the variable for the information that you are passing. If you don't specify txt (or whatever variable name), then you don't have any way of accessing that information.

If you are not passing any information to the function, then it's not required. For example:
function myfunction()
alert("You called myfunction");


 6:30 pm on Apr 9, 2009 (gmt 0)

Right, txt is the variable name. You need some way to reference the parameters that you pass to your functions.

For example, maybe I want to create a function that takes a phone number as the parameter. I might do something like this:

function validatePhoneNum(phoneNumber) {

Within my function, I can now reference the value passed using that phoneNumber variable. Maybe I also want to pass in boolean value to indicate whether or not I allow string data in my phoneNumber:

function validatePhoneNum(phoneNumber, allowStrings) {

So when I call this function like this:

validatePhoneNum("000-0000", false);

I'm passing the value "000-0000" as the phoneNumber variable, and false as the allowString variable. I might call that function again with different values:

validatePhoneNum("1111", true);

Make sense?


 6:35 pm on Apr 9, 2009 (gmt 0)

Ahhh, a birth about to be witnessed..., this is one of the first "stepping stones" to learning how functions work. I remember when I didn't have a clue about function arguments and how they work, believe me..
When you create a function, you can assign special properties to it which are known as the functions arguments. These are contained within the parens () of the function. As in the earlier examples here, txt (a made-up name, with no other significance) is a property of the functions arguments. Think of it as a special kind of variable, which is not global or accessible from outside of the function. In fact, it's easiest to think of it as a special sort of "pipeline" through which you can pass any type of information into the function, be it from another variable, or another function, or just raw data. The "pipeline" analogy is probably the easiest way to understand it:

<script type="text/javascript">
function myfunction(pipeline1,pipeline2,pipeline3) {
var p2_x_p3 = pipeline2*pipeline3;
alert(pipeline1 + p2_x_p3);

myfunction("The value of pipeline2 x pipeline3 is: ",21,67);

Now, you don't even have to assign arguments to the function directly within it's parens (though normally you should if using arguments).
You can still access the functions arguments property (from within the function).
(A function's arguments property is a type of array, but not really like other arrays,
it doesn't have the methods other javascript arrays have, but you do access it's members by index number).
function myfunction2() { //no arguments properties assigned to any names within the functions parens ()
if (arguments[0]) {
if (arguments[1]) {

myfunction2("This is myfunction2's arguments[0]", "This is arguments[1]");

Hopefully that should help further your understanding of this, enjoy!


 6:44 pm on Apr 9, 2009 (gmt 0)

If you don't specify txt (or whatever variable name), then you don't have any way of accessing that information.

Note from my example about accessing the arguments property by index number, that the above quote is not quite correct, as you can still access by arguments index number, though this is less desirable than having named the arguments within the parens.


 7:02 pm on Apr 9, 2009 (gmt 0)

A-ha! I get it now! Thank you, everyone.

@LifeinAsia: your comment about information being passed to the function made me re-examine the flow of the code. Now I see that
sends the value of
function myfunction(txt)
and then from there
function myfunction(txt)
passes that value on to

Got it!

@Fotiman: This comment also helped to clarify:
"You need some way to reference the parameters that you pass to your functions."

@astupidname: Thanks for the encouraging words and example.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved