📎 Webclip
Modern JavaScript, 10 things you should be using, starting today
The article introduces ten ES6+ JavaScript features the author recommends for everyday use, especially for developers returning to or starting with modern JavaScript. For each feature, it contrasts ES5-style patterns with newer syntax, showing how spread/rest parameters, template literals, shorthand/method properties, destructuring, newer array methods, promises with async/await, ES modules, and arrow functions simplify code and improve readability. It closes by noting these are the author’s favorite features to adopt now.
Reading notes#
- Author positions the piece for both newcomers and developers who have used JavaScript sporadically, stressing that the language has changed and some features should now be used daily.
- Recommends two ES6+ reference resources: ES6 Features (es6-features.org) and MDN’s JavaScript reference.
- Explains the spread operator (
...) as turning arrays or objects into comma-separated lists, with examples of appending one array to another and cloning/augmenting objects instead of manual loops. - Shows that without array spread one would loop over
firstHalfandpushintosecondHalf, and without object spread one would iterateObject.keys(hero)and assign properties manually. - Defines rest parameters as collecting remaining function arguments into an array, contrasting with the older
argumentsobject and demonstrating a function that sums all parameters withreduce. - Highlights that rest parameters both name the collected arguments and make the intent explicit, whereas
argumentshas existed since at least ES5 but is less known. - Presents string interpolation with template literals as a cleaner alternative to concatenating multiline strings with
+and\nin agetDescription()method. - Notes that backticks define multiline strings and
${}inserts expressions into templates, improving readability for complex string construction. - Introduces object shorthand properties by comparing an ES5-style factory function returning
{ x: x, y: y }to an ES6 version returning{ x, y }, emphasizing reduced clutter when key and variable names match. - Describes method properties shorthand: instead of
add: function(a,b) {}, one can writeadd(a,b) {}within an object literal for methods likeadd,sub, andmultiply. - Motivates destructuring as a way to reduce repetitive property access and help developer “sanity” when pulling values from objects and arrays.
- Shows object destructuring of
reqto extractbody.name,body.description, andurlinto named variables in a single line, replacing several individual assignments. - Demonstrates array destructuring to pull specific positions (e.g. first and third element) and collect remaining elements with rest syntax, instead of indexing like
array[0]andarray[2]. - Points out that skipping elements in array destructuring uses extra commas and that rest
...remainingcan gather leftover items into an array. - Illustrates parameter destructuring by changing a function from
doSomething(config)withconfig.a,config.b,config.cchecks todoSomething({ a, b, c }), simplifying access to options. - Lists useful ES6 array methods:
find()to retrieve an item or null,findIndex()for its index,some()to test if any element matches a predicate, andincludes()to check membership. - Provides examples using an array of objects to demonstrate
find,findIndex, andsome(for acheckedflag), plus a numeric array to showincludes. - Recaps the older callback-based pattern for asynchronous code using a
doSomething(cb)function withsetTimeoutand invoking the callback after completion. - Rewrites the same asynchronous behavior using native Promises:
doSomethingreturns anew Promisethat callsresolve('done')after a timeout, then consumed with.then. - Shows promise chaining for sequential async operations (
getUser,getOrderByUser,getOrderItemsByOrder) that pass results through successive.then()calls. - Introduces
async/awaitto make asynchronous flows look synchronous, rewriting the chained calls into anasync function getItems()with successiveawaitstatements. - Uses a
try/catchblock in the async example to illustrate centralized error handling and notes that the async function still returns a promise consumed with.then. - Explains module syntax using
exportandexport defaultinmath.js, defining named exportsaddandsubplus a default export that multiplies two numbers. - Demonstrates importing modules in
main.jswithimport mult, { add, sub } from './math';, calling the imported functions and showing their expected results in comments. - Describes arrow functions as a new function notation, comparing a traditional
function printArray(arr) {}toconst printArray = (arr) => {}. - Gives a one-line arrow function example
const add = (a,b) => a + bwhere the expression result is implicitly returned, and another that returns an object literal using parentheses. - Discusses the lexical
thisbehavior of arrow functions by first showing a problem in asumfunction wherethisinside a regularforEachcallback does not refer to the outer context. - Notes the common workaround of assigning
var self = this;and usingself.totalinside the callback, describing it as a hacky but historically used solution. - Shows how converting the
forEachcallback to an arrow function(item) => { this.total += item; }causesthisinside the callback to correctly reference the outersumfunction’sthis. - Concludes by stating there are many more ES6+ features, but highlights these as the author’s favorite ones to adopt right away.
