Optional parameters in JavaScript
<h2>Question</h2>

<p>What is a good way of assigning a default value to an optional parameter?</p>

<h2>Background</h2>

<p>I'm playing around with <em>optional</em> parameters in JavaScript, and the idea of assigning a default value if a parameter is not specified. My method in question accepts two parameters, the latter of which I deem to be optional, and if unspecified should default to <code>false</code>. My method looks something like this...</p>

<pre><code>// selects the item, appropriately updating any siblings
// item to select [, toggle on / off]
this.selectItem = function(item, toggle)
{
    toggle = toggle &amp;&amp; typeof toggle === 'boolean';
    if (toggle)
    {
       // ...
    }
}
</code></pre>

<h2>Testing</h2>

<p>After running a few tests on <a href="http://jsfiddle.net/nLAAt/2/" rel="nofollow">this jsFiddle</a>, using the following principal for default value assigning:</p>

<pre><code>function checkParamIsBoolean(param)
{
    param = param &amp;&amp; typeof param === 'boolean';
}

checkParamIsBoolean('me'); // boolean
checkParamIsBoolean([]); // boolean
checkParamIsBoolean(true); // boolean
checkParamIsBoolean(false); // boolean
checkParamIsBoolean(1 == 1); // boolean
checkParamIsBoolean(1); // boolean
checkParamIsBoolean(null); // object
checkParamIsBoolean(undefined); // undefined
​
</code></pre>

<p>As you can, the results vary, and aren't desired.</p>

<h2>Expected</h2>

<p><code>null</code> = <code>false</code><br />
<code>undefined</code> = <code>false</code></p>

<h2>Actual</h2>

<p><code>null</code> = <code>object</code><br />
<code>undefined</code> = <code>undefined</code></p>

<h2>Summary</h2>

<p>Are there any alternative approaches to assigning a default value to an optional parameter if it's unspecified; would it be better to use <code>_toggle</code> as the parameter and then assign the value to <code>var toggle</code> within the method?</p>
