Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Required fields*

Skipping optional function parameters in JavaScript

Could you please point me to the nice way of skipping optional parameters in JavaScript.

For example, I want to throw away all opt_ parameters here:

goog.net.XhrIo.send(url, opt_callback, opt_method, opt_content, {'Cache-Control': 'no-cache'}, opt_timeoutInterval)

Answer*

Draft saved
Draft discarded
Cancel
5
  • 1
    +1. I don't know what this does exactly (same as undefined ?) but it sure looks best for "skipping optional parameters". Commented Dec 2, 2011 at 23:01
  • I have found that you can never be sure what to pass instead of "skipped" parameter. Undefined? Null? 0? Empty string? Empty object? Array? If behaviour of omittting the parameter in not docummented, or there is no default parameter, you are in great trouble. You can't sleep well if you update the external library. Only god and maybe library developer can know what will happen if you pass anything unexpected instead of the required parameter. Generaly if this behaviour is not documented, the bugs are on you Commented Apr 3, 2013 at 18:09
  • THis still works in 2017 on the latest google chrome version so I'm upvoting. Commented Mar 30, 2017 at 10:29
  • 1
    Hi, this works well, unless the function uses arguments.length. See this example: argl = function() { return arguments.length; } argl(1) => 1 argl(1, undefined, undefined) => 3 argl(1, undefined, 2) => 3 Commented Aug 31, 2017 at 12:51
  • The npm module oracledb makes heavy use of arguments.length. Mostly to decide if a callback is given. Unfortunately, undefined is not an option to call these methods. One needs introduce an if to choose the right parameter list length wtf Commented May 2, 2019 at 13:27

lang-js

AltStyle によって変換されたページ (->オリジナル) /