Skip to content

ferreiratiago/js-wtf

Repository files navigation

What the f** JavaScript

These are some of the weirdest things brought to us by JavaScript.

Number('0.')    // 0
Number('.0')    // 0
Number('.')     // NaN

Why?

Number({})      // NaN
Number([])      // 0

Why?

Number(undefined)   // NaN
Number(null)        // 0

Why?

Number('0O0')       // 0
Number('0X0')       // 0

Why?

Number.MAX_VALUE > 0;   //true
Number.MIN_VALUE < 0;   //false

Why?

42.toFixed(2)       // SyntaxError: Invalid or unexpected token
42. toFixed(2)      // SyntaxError: Unexpected identifier
42 .toFixed(2)      // '42.00'
42 . toFixed(2)     // '42.00'
42.0.toFixed(2)     // '42.00'
42..toFixed(2)      // '42.00'
(42).toFixed(2)     // '42.00'

Why?

Math.max() > Math.min()   // false

Why?

[] + {}     // '[object Object]'
{} + []     // 0

Why?

true + true === 2   // true
true - true === 0   // true
true === 1          // false

Why?

0.1 + 0.2 === 0.3   // false

Why?

1 < 2 < 3   // true
3 > 2 > 1   // false

Why?

'5' + 3     // '53'
'5' - 3     // 2

Why?

String(-0)      // '0'
Number('-0')    // -0

Why?

String(null)        // 'null'
String([null])      //  ''

String(undefined)    // 'undefined'
String([undefined])  //  ''

Why?

String({})      // '[object Object]'
String([])      // ''

Why?

var s = Symbol('Hello World')   // Symbol(Hello World)

String(s)   // 'Symbol(Hello World)'
s + ''      // TypeError: Cannot convert a Symbol value to a string

Why?

parseInt('15')              // 15
parseInt('15 and more')     // 15
parseInt('more and 15')     // NaN

Why?

(!+[]+[]+![]).length    // 9

Why?

[] == ![]   // true

Why?

Array.apply(null, Array(3))        // [ undefined, undefined, undefined ]
Array.apply(null, [,,,])           // [ undefined, undefined, undefined ]
Array.apply(null, {length : 3})    // [ undefined, undefined, undefined ]
Array.apply(null, (a,b,c) => {})   // [ undefined, undefined, undefined ]

Why?

[10, 9, 8, 3, 2, 1, 0].sort() // [ 0, 1, 10, 2, 3, 8, 9 ]

Why?

function () { console.log('Hello World') }()       // SyntaxError: Unexpected token (

Why?

function foo() { console.log('Hello World') }()  // SyntaxError: Unexpected token )

Why?

!function () { console.log('Hello World') }()  // Hello World

Why?

function foo() {
    return
        'Hello World';
}

foo()   // undefined

Why?

switch (42) {
    default:
        console.log('foo');
    case 10:
    case 20:
        console.log('bar');
        break;
    case 30:
        console.log('zed');
        break;
}
// foo
// bar

Why?

function foo() {
    try {
        return 2
    } finally {
        return 3
    }
}

foo()   // 3

Why?

function foo(x = {y: 10}, {y = 20} = {}) {
    console.log(x.y, y);
}

foo();                  // 10 20
foo({y: 30}, {y: 40});  // 30 40
foo({}, {});            // undefined 20

Why?

(function(a = b, b) {

}(undefined, 1));   // ReferenceError

Why?

let b = 1;

(function(a = b, b) {
    console.log(a, b);
}(undefined, 2));   // ReferenceError

Why?

{
    typeof a;   // undefined
    typeof b;   // ReferenceError

    let b;
}

Why?