some of JavaScript’s dirty tricks
¶ by !undefinedA couple of quick ones:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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?
Leave a Reply