Skip to main content

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*

19
  • 84
    I'd user triple equals ('...lArg === "und...') to be on the safe side. Commented Mar 5, 2012 at 22:28
  • 36
    It's probably best to make using === a habit so you don't have to remember under which circumstances it's 'safe'. Commented Oct 31, 2012 at 20:38
  • 46
    Why use a ternary operator here? Isn't the 2nd clause just setting optionalArg = optionalArg?. I would think just a simple IF would do: if(typeof optionalArg === "undefined") optionalArg = "defaultValue"; Commented Dec 19, 2012 at 18:50
  • 14
    I think it signals the intent better - typeof doesn't evaluate the operate, only inspects its type, which is what we're interested in. Also, evaluation will be around 40% slower (jsperf.com/stackoverflow148901) Commented Feb 5, 2013 at 17:56
  • 13
    Well, you aren't talking about optional parameters - you really are passing a parameter, so this solution doesn't apply to your case. NaN doesn't mean "undefined" - it means you've passing a number but its value is NaN. See stackoverflow.com/questions/3215120/… for more Commented Mar 22, 2013 at 10:36