Posts

Showing posts from January, 2017

Javascript Absurdities

Javascript is a funky language. Here are some obvious design flaws: **1. [Regex `test()` method is inconsistent when global flag is set](http://stackoverflow.com/questions/1520800/why-regexp-with-global-flag-in-javascript-give-wrong-results)** ``` var pattern = /hi/g; pattern.test('hi'); //true pattern.test('hi'); //false ``` Today at work I spent hours on a witch hunt trying to pinpoint the cause of a bug that was delaying a production release. It ended up coming down to this absurd Javascript quirk within a single "if" statement inside a portion of code that hadn't been touched in a year. **2. null and undefined** Having separate null and undefined types just adds unnecessary confusion. The best way to check whether variable is null or undefined is `if (varname == null)`. Using the triple equals (which is preferred in almost every other situation) will not catch both types. **3. typeof** ``` typeof [] //"object" typeof nul