Skip to content

Commit

Permalink
Getting rid of parseInt -- it's generally better to use either parseF…
Browse files Browse the repository at this point in the history
…loat(), Number() or simply the unary plus operator
  • Loading branch information
padolsey authored and rmurphey committed Jun 17, 2010
1 parent eeb91fb commit 3df5d95
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion book/src/javascript-basics.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,16 @@ console.log(foo + bar); // 12. uh oh</programlisting>
var bar = '2';

// coerce the string to a number
console.log(foo + parseInt(bar));</programlisting>
console.log(foo + Number(bar));</programlisting>
</example>

<para>The Number constructor, when called as a function (like above) will have the effect of casting its argument into a number. You could also use the unary plus operator, which does the same thing:</para>

<programlisting>
// coerce the string to a number (using a unary-plus operator)
console.log(foo + +bar);</programlisting>
</example>

</section>

<section>
Expand Down

0 comments on commit 3df5d95

Please sign in to comment.