The best of wtfjs

A collection of my favorite wtfjs moments. With deepest respect, gratitude, and suspicious fear for all the contributers hackers featured on wtfjs.com whom made this exploration possible. - @brianleroux, Dec 2012, Paris.

Truthiness.

Basic boolean operation. This might seem obvious but we need to start from a common ground. Stuff gets messy later on.


true
!true
!(true)
!!(true)

Double equals, aka the Abstract Equality Operator is trecherous good fun.


[] == false
"" == false
null == false

And, it turns out, we can coerce true to 1.


+true
true + 1
true + true === 2

It gets better.


"0" && {} 
0 && {}
0 == "0"
null == undefined

One last beauty. It turns out undefined is mutable.


undefined = 42
typeof a === 'undefined'

String


"I am a string" instanceof String

String comparison is common business.


typeof "abc" == "string" 
typeof String("abc") == "string"
String("abc") == "abc"  

Instantiation via new operator can yield interesting results!


"I am a string" instanceof String
String("abc") instanceof String
(new String("abc")) instanceof String
String("abc") == (new String("abc"))

One last fun one. I think everyone has seen this in a template at some point.


("foo" + + "bar")

Another awesome hack, find out the [[Class]] of a thing.


Object.prototype.toString.call([1,2,3]) // [object Array]
{}.toString.call(new Date) // [object Date]

Number and maths.

JavaScript has only one Number type, represented as IEEE 754 Double Precision floating point. That's a fancy way of saying cannot represent decimal fractions with acurracy. Ouch!


9999999999999999
0.1 + 0.2 === 0.3

1 === 1
Number.prototype.isOne = function () { return this === 1; }

// to fix..
Number.prototype.isOne = function () { return +this === 1 }

One of my all time favorites


3 > 2 > 1

// heres why
(3 > 2)
true > 1 

// so the "solution" is
3 > 2 >= 1

Coercion from a String to a Number is a fantastic source of code humour.


"2" +1
"2" -1
"222"- -"111"
"222" - -1

Its hard to forget parseInt accepts a second argument for radix.


parseInt('06')
parseInt('08')

There are other ways to cast a Number. Not all behave alike. (Surprise!)


Number('Infinity')
parseFloat('Infinity')
parseInt('Infinity')

I assume to disambiguate, you cannot always call methods on whole Number literals.


42.toFixed(2)
42.888.toFixed(2)
// or can you!
42..toFixed(2)

This is just a nice shortcut.


Math.round(4.2) === ~~4.2

Infinity is an interesting concept.


Infinity === 1/0
Infinity - 1
1 - Infinity
0 === -0 
1/0 === 1/-0 // hmm.

I am comforted by a maximum before Infinity.


Number.MAX_VALUE*1.0000000000000001
Number.MAX_VALUE*1.0000000000000002

I bet you wondered...


Number.MIN_VALUE > 0

Math also has min/max methods.


Math.max(0,1,2)
Math.max() > Math.min()

Math is really not sure of itself.


Math instanceof Math

Of course if we're comparing Number to things knowing if it is NaN is useful.


typeof NaN 
NaN instanceof Number
isNaN(null)
isNaN(null) == 0
+null
NaN === NaN

Date and time.

This is a 'clever' hack that might have you asking wtf.


Date.now() === +new Date

This is true wtf material.


d = new Date('silent failures are the best')
d.getTime()

Array and Object.

Initialized an array with four undefined values which coerced into empty strings which then was cast to a string by == (Array.prototype.toString calls Array.prototype.join).


",,," == new Array(4)

// nothing to do with Array but the comma operator is fun
[0,1,2][0,1,2]

Abstract equality coercion on Array is fun!


[] == []
[] == ![]
[] == 0
+[] === 0  // true, wait whu?
+([] == 0)

More fun?


2 == [2]
2 === Number([2].valueOf().toString())

Object has an odd relationship to null.


typeof null // object 
null instanceof Object // false

Function.


// arguments object not array like; can be leaky
var args = [].slice.call(arguments)

0xfff.constructor
var c = "constructor"
c[c][c]("console.log('wtfjs!')")()

// constructor by the new operator super important
function b() {}
b() instanceof b // false! ruh roh. `this` is the global in b.
new b() instanceof b // true

// anyhow, we can fix `this` (get it?)
function b() {if (!(this instanceof b)) return new b }
b() instanceof b