↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

11 Powerful JavaScript One-Liners Worth Knowing

The page collects 11 short JavaScript utilities and shows the single-line code for each one. The examples cover string counting, object emptiness, async waiting, date differences, redirects, touch support, DOM insertion, shuffling, selected text, random booleans, and array averages.

Reading notes
#

  • characterCount splits a string by a given character and uses the array length minus one.
  • isEmpty checks that an object has no own keys and that its constructor is Object.
  • wait creates a promise and resolves it with setTimeout after a given time.
  • daysBetween converts the absolute difference between two dates from milliseconds into days.
  • redirect sets location.href to send the browser to another URL.
  • touchSupported checks whether the document supports touchstart.
  • insertHTMLAfter uses insertAdjacentHTML('afterend', html) to place HTML after an element.
  • shuffle uses arr.sort(() => Math.random() > 0.5) to randomize array order.
  • getSelectedText returns the browser selection as a string.
  • getRandomBoolean returns true or false with equal probability.
  • average sums array values with reduce and divides by arr.length.