obfuscated fibonacci

feb 12 , 2013
  var fib = function (_) {
      for(_=[+[],++[[]][+[]],+[],_],_[++[++[++[[]][+[]]][+[]]][+[]]]=(((_[++[++[++[[]][+[]]][+[]]][+[]]]-(++[[]][+[]]))&(((--[[]][+[]])>>>(++[[]][+[]]))))===(_[++[++[++[[]][+[]]][+[]]][+[]]]-(++[[]][+[]])))?(_[++[++[[]][+[]]][+[]]]=++[[]][+[]],_[++[++[++[[]][+[]]][+[]]][+[]]]-(++[[]][+[]])):+[];_[++[++[++[[]][+[]]][+[]]][+[]]]--;_[+[]]=(_[++[[]][+[]]]=_[++[++[[]][+[]]][+[]]]=_[+[]]+_[++[[]][+[]]])-_[+[]]);
      return _[++[++[[]][+[]]][+[]]];
  }

  console.assert(fib(-1) === 0);
  console.assert(fib(0) === 0);
  console.assert(fib(1) === 1);
  console.assert(fib(2) === 1);
  console.assert(fib(3) === 2);
  console.assert(fib(4) === 3);
  console.assert(fib(5) === 5);
  console.assert(fib(6) === 8);
  console.assert(fib(7) === 13);
  console.assert(fib(32) === 2178309);
  console.assert(fib(46) === 1836311903);
  console.assert(fib(47) === 2971215073);
  console.assert(fib(63) === 6557470319842);

  console.log('done!');

I've combined (for fun) a few WTFs to make something bigger.

Just in case... don't code in JS like that. Seriously.

-- @s3a


magic function properties

feb 5 , 2013
    var f = function() { };
    f.foo = 'foo'; // sets f.foo to 'foo'

Functions are objects, so you can set properties on them after creation.

    f.name; // is ''
    f.name = 'foo';
    f.name; // is still ''

But not all the time.

    var f = function myFunction() { };
    f.name; // is 'myFunction'

Functions happen to have magic properties. One of them is the non-standard .name, which stores the function's first name and read-only.

@tmcw


array comparison

jan 28 , 2013

Did you know that JavaScript can compare arrays using lexicographical ordering?

  [1, 2, 4] < [1, 2, 5]  // true
  [1, 3, 4] < [1, 2, 5]  // false

Just don't expect trichotomy to hold.

  [1, 2, 3] === [1, 2, 3]   // false
  [1, 2, 3] <   [1, 2, 3]   // false
  [1, 2, 3] ==  [1, 2, 3]   // false
  [1, 2, 3] >   [1, 2, 3]   // false

Oh, and just in case you're wondering, it knows it's messing with you.

  [1, 2, 3] <= [1, 2, 3]   // true
  [1, 2, 3] >= [1, 2, 3]   // true

@pwnall


undefined props on numbers

dec 28 , 2012
    function getBounds(node) {
        var n = node || 0;
        return { width: n.width, height: n.height };
    }

If you call getBounds() with null, you'll get back { width: undefined, height: undefined } because in JS, numbers have properties (inherited from Number), and using an undefined property doesn't throw, it just returns undefined. null and undefined are propertyless. Definitely not what I expected in a quick reading of code like this.

@unwiredben


infinity madness

oct 10 , 2012
    parseFloat( 'Infinity' ) // returns Infinity
    Number( 'Infinity' ) // returns Infinity
    parseInt( 'Infinity' ) // returns NaN, of course. With any radix passed as well.

@fivetanley


Fork me on GitHub