Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>UPD</strong> (from my comment): It is certainly not a good practice to pick some magic number to indicate value absence, unless you can be completely sure this magic number would not appear as a result of some arithmetics (which generally is true). So the best approach here is either to use a wider type than <code>int</code> (e.g. <code>Number</code>), or pass some flag (<code>Boolean</code> value) to function, as well as <code>int</code>, to indicate actual value absence. Choice should depend on whether you want to emphasize the importance of fact that passed value is integral.</p> <hr> <p>If you feel comfortable with expanding argument type, then you should use <code>NaN</code> and do this:</p> <pre><code>function setMyValue (myVar : Number = NaN) : void { if ( isNaN(myVar) ) { /* Generate a value for myVar */ } else { /* Use the supplied value for myVar */ } } </code></pre> <p>That is both more technically accurate and it is also more stable. It will also allow you to strongly type at compile-time and would not cause weird bugs as in case of using <code>Object</code>.</p> <p>Note that this will not work with <code>int</code> as one of the answers suggests. <code>int</code> has <em>only</em> numerical values, and <code>NaN</code> will get coerced to 0. You will need to use <code>Number</code> type. </p> <p>If you're worried about performance - don't be. Using <code>Number</code> instead of <code>int</code> or <code>uint</code> is generally OK.</p> <hr> <p><strong>UPD:</strong> If you want to explicitly mark that argument should be integral just use additional flag and exact type - it would be much clearer.</p> <pre><code>function setMyValue (isIntValueSupplied : Boolean = false, intValue : int = 0) : void { if ( isIntValueSupplied ) { /* Generate or use a default value of intValue */ } else { /* Use the supplied value for intValue */ } } </code></pre> <hr> <p>Another option is to define your own type with additional value, e.g. <code>MaybeInt</code>, which holds integer value and a flag (whether it was actually initialized or not). That way you would clearly signal that value should be integral or <em>no-value</em>. However, this approach may be a bit bulky.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload