Extracting complex logic to named functions

If a piece of code looks cryptic and is difficult to understand, put it in a function. By giving that function a descriptive name, your code becomes much more readable. A good name can make a long comment unnecessary.

// We can get today’s date in a specific timezone with `.toLocaleString()`.
// The code looks complicated, and we might not remember what it does two
// months from now.

const now = new Date()
const nowAsString = now.toLocaleString('en-US', {
  timeZone: 'America/Los_Angeles'
// ⇒ '8/15/2020'

// Putting this code into a function makes it easily reusable. We don’t need
// to write a comment for it because the name already explains what it does.
// It’s okay to condense the code and skip a few variables in this case.

const getDateInLosAngeles = () => (new Date()).toLocaleString('en-US', {
  timeZone: 'America/Los_Angeles'

// Getting the date with one function call is more convenient and readable.
// ⇒ '8/15/2020'