Destructuring the console object
Just as we can destructure all other objects, we can destructure functions like log() and warn() directly out of the console object.
Read fire tipThese are a collection of tips and tricks you can use to improve the performance and readability of your code.
Just as we can destructure all other objects, we can destructure functions like log() and warn() directly out of the console object.
Read fire tipTo find the largest value in an array of numbers, we can spread that array into Math.max() instead of manually iterating over it.
Read fire tipWe cannot use the typeof operator in JavaScript to check if something is an array or not. Luckily, there is a helper function on the Array prototype for that.
Read fire tipLookup objects can replace long if-else branches and switch statements. We can make them more resilient by checking if a value exists for a given key.
Read fire tipFinding an object with a specific property is slow in large arrays. We can speed this operation up significantly by transforming the array to a lookup object.
Read fire tipWhen a feature calls for the first five photos from a list and a link that says “27 more photos”, we can split the list of photos into these two blocks.
Read fire tipInfinity is just a number, dude. Most calculations JavaScript lets us do with Infinity will still return Infinity. Some no longer return numbers.
Read fire tipWith JavaScript’s built-in formatter for relative timestamps, we can build strings like “2 months from now” without having to use third party libraries.
Read fire tipBy combining regular expressions and parseFloat, we can create a helper function that extracts all float values from a text input.
Read fire tipSyntactic sugar on the Array prototype’s map function and type constructors being functions allow us to quickly map values from one type to another.
Read fire tipThe ternary operator can be used at many levels. The further we move it into a statement, the more duplication it can save us.
Read fire tipThe String prototype’s replace function only replaces the first occurrence of a substring by default. We can extend that with a global flag on the expression.
Read fire tipRegular JavaScript has no concept of required parameters. We can use default parameter values to emulate this feature.
Read fire tipIn JavaScript, we can remove the largest value from an array of numbers in two steps: we first need to find that number, then we can filter it from the array.
Read fire tipBy combining map and slice, we can write a helper function to split large arrays into many similarly sized blocks. This is useful for features like pagination.
Read fire tipWhen the if-branch already returns, we can omit the keyword else. The execution will only go beyond the if-branch if it doesn’t apply anyways.
Read fire tipTo improve the readability of React components, we can import the styled-components they use from another file under a ui-namespace.
Read fire tip