joewagner / javascript-idiosyncrasies

A bunch of Javascript idiosyncrasies.

Home Page:https://github.com/miguelmota/javascript-idiosyncrasies

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JavaScript Idiosyncrasies

This is a growing collection of JavaScript idiosyncrasies, or things that are not easily understood.

Good for interview questions. If you can explain why the answer is to all questions then you are truly a JavaScript ninja. Some of these are just to confuse the reader, and by no means encourage best practices and should be never be seen in production code. It's simply to demonstrate if one has a strong grasp of the language and knows what I'm trying to get at.

Feel free to submit pull requests :)


Q. What's the result?

(function() {
    var foo = new Object();
    var bar = new Object();
    var map = new Object();

    map[foo] = 'foo';
    map[bar] = 'bar';

    return map[foo];
})();

A.

"bar"

JSBin

Q. What's the result?

function f() {
    return 'foo';
}
(function() {
    if (1 == 0) {
        function f() {
            return 'bar';
        }
    }
    return f();
})();

A.

"bar"

JSBin

Q. What's the result?

(function() {
    return NaN !== NaN;
})();

A.

true

JSBin

Q. What's the result?

(function() {
    function foo() {
        return 'a';
    }

    return foo();

    function foo() {
        return 'b';
    }
})();

A.

"b"

JSBin

Q. What's the result?

(function(limit) {
  for (var i = 0; i < limit; i++) {
    setTimeout(function() {
      console.log(i);
    }, 0);
  }
})(3);

A.

3
3
3

JSBin

Q. What's the result?

(function() {
    return ~(-3);
})();

A.

2

JSBin

Q. What's the result?

(function() {
    return ~~(-3.4);
})();

A.

-3

JSBin

Q. What's the result?

(function() {
    return (function (a, b) {}).length;
})();

A.

2

JSBin

Q. What's the result?

(function(num) {
    return ~~num;
})(1.5);

A.

1

JSBin

Q. What's the result?

(function(x) {
    return !!x
})('a');

A.

true

JSBin

Q. What's the result?

(function() {
    return typeof null === 'object';
})();

A.

true

JSBin

Q. What's the result?

(function() {
    this.bar = 'a';

    function qux(bar) {
        this.bar = this.bar || bar;
    }

    function foo(bar) {
        return this.bar || bar;
    }

    return (function(bar) {
        return foo.bind(this)(bar);
    }).call(new qux('b'), 'c');
})();

A.

"b"

JSBin

Q. What's the result?

(function() {}
    var foo = ['a','b','c','d'];
    var bar = ['z','y','x'];
    foo.splice.apply(foo, [2, 1].concat(bar));
    return foo;
})();

A.

["a", "b", "z", "y", "x", "d"]

JSBin

Q. What's the result?

(function() {
    return +(new Date())
})();

A.

1393812837139

JSBin

Q. What's the result?

(function() {
    return (new Date()).valueOf();
})();

A.

1393812845834

JSBin

Q. What's the result?

(function() {
    return (new Date()).toString();
})();

A.

"Sun Mar 02 2014 18:14:01 GMT-0800 (PST)"

JSBin

Q. What's the result?

(function() {
    var foo = 'a';
    (function(foo) {
        foo = 'b';
    })(foo);
    return foo;
})();

A.

"a"

JSBin

Q. What's the result?

(function() {
    function foo(qux) {
        return qux || this.foo;
    }

    return (Function.bind.bind(Function.call)(foo))({foo: 'foo'}, 'qux');
})();

A.

"qux"

JSBin

Q. What's the result?

(function() {
    return arguments.toString();
})();

A.

"[object Arguments]"

JSBin

Q. What's the result?

(function() {
    function always(val) {
        return function() {
            return val;
        };
    }

    var f = always(function(){});
    var g = always(function(){});

    return f() === g();
})();

A.

false

JSBin

Q. What's the result?

(function() {
    var num1 = 10;
    var num2 = new Number('10');
    return num1 === num2;
})();

A.

false

JSBin

Q. What's the result?

(function() {
    return [1+1] === [2];
})()

A.

false

JSbin

Q. What's the result?

(function() {
    var obj1 = { foo: 'bar' };
    var obj2 = { foo: 'bar' };
    return obj1 === obj2;
})();

A.

false

JSBin

Q. What's the result?

(function() {
    return ['10','10','10','10'].map(parseInt);
})();

A.

[10, NaN, 2, 3]

JSBin

Q. What's the result?

(function() {
    var o = {
        toString: function() {
            return 'a';
        },
        valueOf: function () {
            return 1;
        }
    };

    return o+o;
})();

A.

2

JSBin

Q. What's the result?

(function() {
    function Foo() {}

    function Bar() {}
    Bar.prototype.constructor = Bar;

    Bar.prototype = new Foo();

    return (new Bar()).constructor;
})();

A.

function Foo() {}

JSBin

Q. What's the result?

(function() {

  var f = function g() {
    return 1;
  };

  return g();

})();

A.

ReferenceError: g is not defined

JSBin

Q. What's the result?

(function() {

  return void (1+1);

})();

A.

undefined

JSBin

Q. What's the result?

(function() {

  var a = [1,2];
  var b = a;

  a = [1,2];

  return a === b;

})();

A.

false

JSBin

Q. What's the result?

(function() {
    return {foo: 'bar'} === {foo: 'bar'};
})();

A.

false

JSBin

Q. What's the result?

(function() {
    var questionable = 'outer';

    return (function () {
        return questionable;

        var questionable = 'inner';
    })();
})();

A.

undefined

JSBin

Q. What's the result?

(function() {
    return new String('foo') === 'foo';
})();

A.

false

JSBin

Q. What's the result?

(function(n) {
    var even = function (num) {
        return (num === 0) || !(even(num - 1))
    };

    var _even = even;

    even = void 0;

    return _even(n);
})(2);

A.

TypeError: undefined is not a function

JSBin

Q. What's the result?

(function() {
    var n;

    function name() {
        return this.name
    };

    n = name.bind({name: 'foo'});
    n.bind({name: 'bar'})

    return n();
})();

A.

"foo"

JSBin

Q. What's the result?

(function() {
    return ('3' > '12') === ('03' > '12');
})();

A.

false

JSBin

Q. What's the result?

(function() {
    return Math.pow(2,53) === (Math.pow(2,53) + 1);
}();

A.

true

JSBin

Q. What's the result?

(function() {
    return Math.pow(2,1024) === Infinity;
}();

A.

true

JSBin

Q. What's the result?

(function() {
    return (Infinity - 100) === Infinity;
}();

A.

true

JSBin

Q. What's the result?

(function() {
    return (0.1 + 0.2 === 0.3);
})();

A.

false

JSBin

Q. What's the result?

(function() {
    return parseFloat('3.3.4');
})();

A.

3.3

JSBin

Q. What's the result?

(function() {
    return 010;
})();

A.

8

JSBin

Q. What's the result? (assuming window scope)

var declared = 1;
Object.getOwnPropertyDescriptor(window, 'declared').configurable;

A.

false

Q. What's the result? (assuming window scope)

declared = 1;
Object.getOwnPropertyDescriptor(window, 'declared').configurable;

A.

true

Weird parts of JavaScript

Q. What's the result?

(function() {
    return ![];
})();

A.

false

JSBin

Q. What's the result?

(function() {
    return +[];
})();

A.

0

JSBin

Q. What's the result?

(function() {
    return +[![]];
})();

A.

NaN

JSBin

Q. What's the result?

(function() {
    return [][[]];
})();

A.

undefined

JSBin

Q. What's the result?

(function() {
    return +!+[];
})();

A.

1

JSBin

Q. What's the result?

(function() {
    return []+[];
})();

A.

""

JSBin

Q. What's the result?

(function() {
    return true + 1;
})();

A.

2

JSBin

Q. What's the result?

(function() {
    return 0.06 + 0.01;
})();

A.

0.06999999999999999

JSBin

Q. What's the result?

(function() {
    return (parseInt('10000000000000000', 10) <
            parseInt('10000000000000001', 10)
    );
})();

A.

false

JSBin

Q. What's the result?

(function() {
    return (0.1).toFixed(20);
})();

A.

"0.10000000000000000555"

JSBin

Q. What's the result?

(function() {
    return 1 / '';
})();

A.

Infinity

JSBin

Q. What's the result?

(function() {
    return 0 / '';
})();

A.

NaN

JSBin

Q. What's the result?

(function() {
    return 1 * null;
})();

A.

0

JSBin

Q. What's the result?

(function() {
    return new Array() == false;
})();

A.

true

JSBin

License

Released under the MIT License.

About

A bunch of Javascript idiosyncrasies.

https://github.com/miguelmota/javascript-idiosyncrasies

License:MIT License