Jump to content

Archived

This topic is now archived and is closed to further replies.

Kris

Setting argument defaults for a function?

Recommended Posts

When creating a function in PHP, you can set default values for arguments within a function like so:
[code]
my_function($name="unknown",$age) {
    echo "Hello, my name is $name, I am $age";
}[/code]

I am trying to do the same for a function in JavaScript, but don't appear to be having much luck - Google isn't being very helpful either...

Thanks guys

Share this post


Link to post
Share on other sites
Such an option doesn't exist... in fact, I've never seen anything like this.  Generally, it's preferable to simply default the values at the top of the subroutine; so for JS:

[code]function my_function( name, age ) {
  if( name == null ) name = "unknown";
  alert( 'Hello, my name is ' + name + ', I am ' + age );
}[/code]

Share this post


Link to post
Share on other sites
*Slaps forehead*

I was overlooking the obvious there! Many thanks Fenway :)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.