Six nifty ES6 tricks
In this blog post, I show six tricks enabled by new ES6 features. At the end of each section, I point to related material in my book “ Exploring ES6 ” (which is free to read online). Enforcing mandatory parameters via parameter default values ES6 parameter default values are only evaluated when they are actually used. That lets you enforce that a given parameter be provided: /** * Called if a parameter is missing and * the default value is evaluated. */ function mandatory() { throw new Error('Missing parameter'); } function foo(mustBeProvided = mandatory()) { return mustBeProvided; } The function call mandatory() is only made if the parameter mustBeProvided is missing. Interaction: > foo() Error: Missing parameter > foo(123) 123 More information: Sect. “ Required parameters ” in “Exploring ES6” Iterating over Array indices and elements via the for-of loop Method forEach() lets you iterate over the elem...