ECMAScript 5 was finalized over five years ago and it is well supported in all modern browsers. So, with ECMAScript 6 nearing standardization, let’s talk about ECMAScript 5.
Do you know what features were added in ECMAScript 5?
Do your target browsers support ECMAScript 5 yet?
Here are a variety of other features that were added to ECMAScript 5, in addition to array helper functions:
String.prototype.trim, returns a copy of the string with whitespace removed from the beginning and end of the string
parseIntno longer treats strings starting with
0as octal values
Strict mode, Object.create, Object.seal, Object.freeze, getters/setters, and more were also added in ECMAScript 5
Underscore.js vs Native
I try to use native array methods instead of their Underscore.js equivalents whenever possible. However, these native array methods don’t work in all cases.
Not all enumerables are arrays
Here’s two enumerable objects which are not arrays:
- NodeList objects in the browser
- File buffers in Node.js
You cannot use array methods like
forEach on these enumerables, but you can the Underscore.js equivalents like
So why not use Underscore for everything?
Native methods are more understandable
Underscore is less portable
Underscore doesn’t come bundled with your rendering engine.
Native methods are future-proof
Native methods can be faster
As browser engines improve their optimization of native code, your code improves in speed. Underscore’s equivalents of these methods cannot be further optimized at the low-level as their native equivalents.
This claim as a theoretical one. Here’s some jsPerf links to test the real performance difference:
What browsers support ECMAScript 5?
ECMAScript 5 is fully supported in Internet Explorer 10 and nearly fully supported in IE9. Modern versions of Chrome and Firefox also fully support ECMAScript 5.
What if you need to support IE7 or IE8?
ECMAScript 5 all the things!
With these pollyfills in place, you should be able to use the majority of ECMAScript 5 features everywhere. Go forth and ES5-ify your code!