found drama

get oblique

some of JavaScript’s dirty tricks

by !undefined

A couple of quick ones:

// Date -> Number:
var d = +(new Date());
// 1281743590756
typeof d;
// number
// almost anything -> String
d += '';
// '1281743590756'
typeof d;
// string
// inspired @jdalton - https://twitter.com/#!/jdalton/status/90587625869680640
[value] == 'value';
// true
// take advantage of Array's valueOf (i.e., toString)
// anything -> Boolean
d = !!d;
// true
typeof d;
// boolean

What are your favorite type coercions?

About !undefined

Syndicated content from the !undefined Tumblr blog where Rob Friesel posts items related to software engineering, user interface/experience design, and Agile software development. Lots of JavaScript here. View all posts by !undefined →

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*