Skip to content

Commit

Permalink
Math.max() conversions post by Gonçalo Morais
Browse files Browse the repository at this point in the history
Close #73.

Signed-off-by: DTrejo <david.trejo@dena.jp>
  • Loading branch information
gnclmorais authored and DTrejo committed Sep 6, 2013
1 parent 31d2e1c commit 2bb4760
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -91,3 +91,4 @@ Ordered by date of first contribution.
- [Thaddee Tyl aka `espadrine`](https://github.com/espadrine)
- [Stanley Stuart aka `fivetanley`](https://github.com/fivetanley)
- [Ben Combee aka `unwiredben`](https://github.com/unwiredben)
- [Gonçalo Morais aka `gnclmorais`](https://github.com/gnclmorais)
34 changes: 34 additions & 0 deletions posts/2013-08-07-Math.max()-behaviour.md
@@ -0,0 +1,34 @@
`Math.max()` has an interesting behaviour, handling different **JavaScript data
types** in different ways.

<code>
Math.max(3, 0); // 3
Math.max(3, {}); // NaN
Math.max(3, []); // 3
Math.max(3, true); // 3
Math.max(3, 'foo'); // NaN
Math.max(-1, null); // 0
Math.max(-1, undefined); // NaN
</code>

Now, let's focus on **Booleans**:

<code>
Math.max(1, true); // 1
Math.max(0, true); // 1
Math.max(1, false); // 1
Math.max(-1, true); // 1
Math.max(-1, false); // 0
</code>

And now, on **Arrays**:

<code>
Math.max(-1, []); // 0
Math.max(-1, [1]); // 1
Math.max(-1, [1, 4]); // NaN
</code>

So next time, watch out for what you pass into `Math.max()`.

[@gnclmorais](http://gnclmorais.com/gnclmorais)

0 comments on commit 2bb4760

Please sign in to comment.