Updated: Jun 17, 2018
Here is a list of some additional good parts that I recommend developers embrace. These are all features and capabilities that are available in the language today, not experimental parts of some future version.
Getters and Setters
Getters and setters have been available in Chrome since day one, in Firefox since version 2, version 3 of Safari, Internet Explorer 9 and up, and in all mobile browsers.
You can explicitly specify, at object creation time, the object that will be the prototype of the newly created object.
You can create objects that have no prototype by specifying null as the prototype. This is something that can’t otherwise be done. This can be useful when using an object as a dictionary, for example.
You can easily specify the properties of the newly created object, including their descriptors: configurable, enumerable, and writable.
Object.create() has been available in all browsers since IE9.
Array.prototype.map() and Array.prototype.reduce()
Iterating over arrays using forEach is a nicer, more modern, and seemingly more functional approach than an old-fashioned for loop. I say “seemingly” because any operation performed inside forEach can only return results via side-effects, or by modifying the original array. However, a more functional approach is to use other iteration methods available for arrays, such as map and reduce. These methods don’t require side-effects, and can treat the original array as immutable. For example, summing an array using forEach requires an external variable to modify:
whereas reduce doesn’t require such a variable:
Both reduce and map have the same browser support as forEach.
Array.prototype.every() and Array.prototype.some()
Another limitation with forEach is that you can’t break out of the loop (and no, using exceptions doesn’t count). As a result, I’ve seen developers either revert back to for loops when needing to be able to break out, or needlessly iterate over extraneous array elements. A better solution exists in the form of the lesser known every and some array iteration methods. every iterates until the provided callback returns false, and some iterates until the provided callback returns true.
Both every and some have the same browser support as forEach.
Object.freeze(), Object.seal(), and Object.preventExtensions()
Freezing or sealing objects can be a very effective mechanism for ensuring proper ownership and utilization of objects. Used in conjunction with strict mode, they allow you to specify that an object is immutable, and any attempt to modify it will fail and be intercepted and reported as an error.
This post was written by Dan Shappir.
You can also follow his insights on Twitter.