zq29 Posted October 22, 2006 Share Posted October 22, 2006 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 More sharing options...
fenway Posted October 22, 2006 Share Posted October 22, 2006 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] Link to comment https://forums.phpfreaks.com/topic/24732-setting-argument-defaults-for-a-function/#findComment-112832 Share on other sites More sharing options...
zq29 Posted October 22, 2006 Author Share Posted October 22, 2006 *Slaps forehead*I was overlooking the obvious there! Many thanks Fenway :) Link to comment https://forums.phpfreaks.com/topic/24732-setting-argument-defaults-for-a-function/#findComment-112864 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.