/    Sign up×
Community /Pin to ProfileBookmark

empty function var’s like php?

How can i do this in a JS function:
PHP:

[code=php]
function myfunk ($var1 = ‘value’) {
}
[/code]

in the function above if myfunk() is called without passing a value the $var1 gets = to “value”… perfect

how do i do that in JS?
i need my JS function to handle being run with and without a passing variable?

i know it can be done, but im not searching the right words in google cause i cant find it

Thanks
-aPeg

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@WolfShadeNov 03.2010 — <i>
</i>function myfunk (v) {
if(!v) {
v = 'value';
}
}

^_^
Copy linkTweet thisAlerts:
@Declan1991Nov 03.2010 — You will also encounter the shorthand very often.function whatever(v) {
v = v || "default";
}
And always be wary with optional parameters, make sure it doesn't break when they are left out.
Copy linkTweet thisAlerts:
@apegauthorNov 03.2010 — perfect... thanks guys
×

Success!

Help @apeg 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...