↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

10 Practical JavaScript Tricks

The post lists small JavaScript shortcuts for working with arrays, conditionals, and objects. It shows how to turn arguments into an array, sum array values with reduce, use short-circuit conditionals and || defaults, resize arrays through length, swap values with destructuring, build dynamic property names, and filter unique values with Set.

It also notes that efficiency matters less than responsibility, and says a website needs to work in all browsers. The closing point recommends using Endtest or similar tools to check that behavior.

Reading notes
#

  • Convert the arguments object into a regular array with Array.prototype.slice.call(arguments).
  • Sum array values with reduce instead of using a loop.
  • Use hungry && goToFridge() to replace a simple if block.
  • Assign a fallback value with arg1 = arg1 || 32.
  • Use the comma operator to evaluate expressions from left to right and return the last value.
  • Change array.length to shorten an array or empty it.
  • Swap two values with array destructuring.
  • Use sort(function() { return Math.random() - 0.5 }) to shuffle an array.
  • Create dynamic object property names with computed bracket syntax.
  • Remove duplicates by wrapping an array in Set and spreading the result.
  • The closing note says responsibility matters more than efficiency.
  • The post says a website needs to work in all browsers and mentions Endtest as a way to check that.