All snippets in “JavaScript

These are a collection of tips and tricks you can use to improve the performance and readability of your code.

August 28, 2020JavaScript

Math with Infinity

Infinity is just a number, dude. Most calculations JavaScript lets us do with Infinity will still return Infinity. Some no longer return numbers.

Read full article
July 26, 2020JavaScript

Merging arrays

There are several ways to concatenate in JavaScript. We can pick the one that is most readable in each situation.

Read full article
July 25, 2020JavaScript

Flattening nested arrays

We can un-nest arrays with a native function now. We can also define how many levels of nesting we want to take out, to a maximum of “all levels”.

Read full article
July 24, 2020JavaScript

Shuffling arrays

With the Fisher-Yates algorithm, we can shuffle an array with true randomness. The likelihood of two shuffles giving the same result is minimal.

Read full article
July 9, 2020JavaScript

Sorting numbers

The default behavior of sorting arrays assumes every value is a string. That leads to unexpected behavior when working with numbers.

Read full article
July 7, 2020JavaScript

Splitting arrays into chunks

By 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 full article
July 5, 2020JavaScript

Logging as objects

This neat little trick makes values logged to the console much more readable with a minor adjustment to how we log it.

Read full article
June 23, 2020JavaScript

Splitting strings

When splitting strings, the sequence of characters we split at does not appear in the result. We can get all letters by splitting at the empty string.

Read full article
June 13, 2020JavaScript

Creating number ranges

We can create a sequence of numbers by spreading the keys of one array into another array. We can then change that range any way we like.

Read full article