Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of writing a function to do this check, you should just be able to use this expression:</p> <pre><code>(number &lt; 0) </code></pre> <p>Javascript will evaluate this expression by first trying to convert the left hand side to a number value before checking if it's less than zero, which seems to be what you wanted.</p> <hr> <h3>Specifications and details</h3> <p>The behavior for <code>x &lt; y</code> is specified in <em>§11.8.1 The Less-than Operator (<code>&lt;</code>)</em>, which uses <em>§11.8.5 The Abstract Relational Comparison Algorithm</em>.</p> <p>The situation is a lot different if both <code>x</code> and <code>y</code> are strings, but since the right hand side is already a number in <code>(number &lt; 0)</code>, the comparison will attempt to convert the left hand side to a number to be compared numerically. If the left hand side can not be converted to a number, the result is <code>false</code>.</p> <p>Do note that this may give different results when compared to your regex-based approach, but depending on what is it that you're trying to do, it may end up doing the right thing anyway.</p> <ul> <li><code>"-0" &lt; 0</code> is <code>false</code>, which is consistent with the fact that <code>-0 &lt; 0</code> is also <code>false</code> (see: <a href="http://en.wikipedia.org/wiki/Signed_zero" rel="noreferrer">signed zero</a>).</li> <li><code>"-Infinity" &lt; 0</code> is <code>true</code> (infinity is acknowledged)</li> <li><code>"-1e0" &lt; 0</code> is <code>true</code> (scientific notation literals are accepted)</li> <li><code>"-0x1" &lt; 0</code> is <code>true</code> (hexadecimal literals are accepted)</li> <li><code>" -1 " &lt; 0</code> is <code>true</code> (some forms of whitespaces are allowed)</li> </ul> <p>For each of the above example, the regex method would evaluate to the contrary (<code>true</code> instead of <code>false</code> and vice versa).</p> <h3>References</h3> <ul> <li><a href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf" rel="noreferrer">ECMAScript 5 (PDF)</a></li> <li><a href="http://bclary.com/2004/11/07/#a-11.8.1" rel="noreferrer">ECMAScript 3, §11.8.1 The Less-than Operator (<code>&lt;</code>)</a></li> <li><a href="http://bclary.com/2004/11/07/#a-11.8.5" rel="noreferrer">ECMAScript 3, §11.8.5 The Abstract Relational Comparison Algorithm</a></li> </ul> <h3>See also</h3> <ul> <li><a href="http://www.regular-expressions.info/floatingpoint.html" rel="noreferrer">regular-expressions.info/Matching Floating Point Numbers with a Regular Expression</a></li> </ul> <hr> <h3>Appendix 1: Conditional operator <code>?:</code></h3> <p>It should also be said that statements of this form:</p> <pre><code>if (someCondition) { return valueForTrue; } else { return valueForFalse; } </code></pre> <p>can be refactored to use the ternary/conditional <code>?:</code> operator (<a href="http://bclary.com/2004/11/07/#a-11.12" rel="noreferrer">§11.12</a>) to simply:</p> <pre><code>return (someCondition) ? valueForTrue : valueForFalse; </code></pre> <p>Idiomatic usage of <code>?:</code> can make the code more concise and readable.</p> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/1688337/javascript-if-alternative">javascript if alternative</a></li> <li><a href="https://stackoverflow.com/questions/160218/to-ternary-or-not-to-ternary">To Ternary or Not To Ternary?</a></li> </ul> <hr> <h3>Appendix 2: Type conversion functions</h3> <p>Javascript has functions that you can call to perform various type conversions.</p> <p>Something like the following:</p> <pre><code>if (someVariable) { return true; } else { return false; } </code></pre> <p>Can be refactored using the <code>?:</code> operator to:</p> <pre><code>return (someVariable ? true : false); </code></pre> <p>But you can also further simplify this to:</p> <pre><code>return Boolean(someVariable); </code></pre> <p>This calls <code>Boolean</code> as a function (<a href="http://bclary.com/2004/11/07/#a-15.6.1" rel="noreferrer">§15.16.1</a>) to perform the desired type conversion. You can similarly call <code>Number</code> as a function (<a href="http://bclary.com/2004/11/07/#a-15.7.1" rel="noreferrer">§15.17.1</a>) to perform a conversion to number.</p> <h3>Related questions</h3> <ul> <li><a href="https://stackoverflow.com/questions/263965/how-can-i-convert-a-string-to-boolean-in-javascript">How can I convert a string to boolean in JavaScript?</a></li> <li><a href="https://stackoverflow.com/questions/856324/what-is-the-purpose-of-javascript-new-boolean">what is the purpose of javascript new Boolean() ?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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