r/javascript • • Mar 18 '19

What's your favorite, most useful javascript one-liner?

385 Upvotes

422 comments sorted by

View all comments

Show parent comments

14

u/zombarista Mar 19 '19 edited Mar 19 '19

Month and hour. For example

let nowMonth = 2; // March 
let futureMonth = nowMonth + 18;
console.log( months[ futureMonth % months.length ] );  // still outputs September, even though there isn't a 20th month

Worth noting that lots of languages refer to their months from 0-11, presumably for cool tricks like this.

1

u/SarahC Mar 19 '19

Where'd yearsFromNow come from?

1

u/zombarista Mar 19 '19

Whoops changed a variable name halfway through the example hoping it would make more sense.