📎 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
argumentsobject into a regular array withArray.prototype.slice.call(arguments). - Sum array values with
reduceinstead of using a loop. - Use
hungry && goToFridge()to replace a simpleifblock. - 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.lengthto 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
Setand 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.
