Jump to content

Setting argument defaults for a function?


zq29

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
Link to comment
https://forums.phpfreaks.com/topic/24732-setting-argument-defaults-for-a-function/
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]

Archived

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

×
×
  • Create New...

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.