false isnt false

mar 6 , 2013
    true == 'true'     // true
    false == 'false';  // false

This is expected behaviour as == doesn't do value equality, but rather it does numeric value equality1, thus 'false' is truethy, thus equals 1

@grnadav


null, undefined and test

feb 28 , 2013

Checking a variable for one word with one to ten letters lowercase only?

Try this regular expression: /^[a-z]{1,10}$/.test('wakaluba').

    /^[a-z]{1,10}$/.test(null);
    /^[a-z]{1,10}$/.test(undefined);

Both should obviously fail, but return true. srsly, WTF JS?

@damienklinnert


parseint radix

feb 22 , 2013
    parseInt('fuck');     // NaN
    parseInt('fuck', 16); // 15

So remember kids, always supply a radix!

@kevincennis


why am i a number

feb 21 , 2013

Can someone tell me?

    "Why am I a " + typeof + ""; // "Why am I a number"

@jhnnns


I think I can tell you.

typeof requires some value or expression that is evaluated and passed then to typeof as an argument. +"" is treated as expression because + operator can be used as an unary operator (syntax is: + UnaryExpression). + converts its operand to Number type. So, + "" is evaluated to 0 and then 0 is passed to typeof.

Note that we can cast strings to numbers by preceding them with +:


(2 + "3");  // 23
(2 + +"3"); // 5
(+"");      // 0

@wojciechfornal


unicode vars

feb 13 , 2013

ECMAScript allows you to use unicode variable names, just use the usual \uXXXX codes:

    var \u1000 = {
      \u1001: 'foo',
      \u1011: 'bar'
    };

    console.log( \u1000.\u1001 ); // prints 'foo'
    console.log( \u1000.\u1011 ); // prints 'bar'

WTF right?

By @manekinekko


Fork me on GitHub