/    Sign up×
Community /Pin to ProfileBookmark

default parameters values

Is there any posibility to put default parameters values in javascript?
in C you can use default parameters values like this:

myFunction(int a=2, int b=3)

if you don´t send any parameter to this function the value of a will be 2 and b will be 3, but if you pass any parameter to the function the variable will get the passed value.

Does anybody know if I can do this in javascript???

Thanks to everybody

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@mrhooApr 10.2006 — [CODE]myFunction(a,b){
if(typeof(a)=='undefined')a=2;
if(typeof(b)=='undefined')b=3;
//
}[/CODE]


The above code allows you to pass null, 0, or '' as a parameter without replacing it. But if there is no value passed, the default is used.

If you always need a non zero value it is even simpler:


[CODE]function imp(wot, wch){
if(!wot) wot= 'html';
if(!wch) wch= '1.0';
var DI= (document)? document.implementation: false;
return (DI && DI.hasFeature)? DI.hasFeature(wot,wch): false;
}[/CODE]
×

Success!

Help @ramonibz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.23,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...