This is my top 10 most used functions when programming

This is my top 10 most used functions when programming

In this post, I list my most often used functions when programming. They are either built in or used in third party libraries such as Lodash for JavaScript.

Nowadays I rarely use for loops. Instead, I find filter and map functions a lot more useful.

Array filter

As the name suggests, you can filter items in an array. This is particularly useful when you want to change the values of an element or retrieve only specific data. When working with data in arrays, this for me, is the most used function.

Array map

Likewise, when I need to change the data in an array, I prefer using Array.map function instead of using for loop. I also find it useful when copying elements from one array to another but with a few different values.

Array reduce

Reduce is an excellent solution for aggregation. Within a few lines, you can calculate the sum of data in an array. When dealing with numbers, filter map and reduce are an effective combos for me.

Array sort

Sort is highly relevant in the frontend when unless you're using third party datatables, you will be sorting data.

Array length

Before referencing items in an array, I check if the array has data with the Array.length function. This helps for instance avoiding undefined errors in JavaScript.

Array isArray

I use the isArray function to check if an API is either returning an array of objects or an object. This is one of the most common pitfalls I encounter when fetching and using data from APIs.

Array includes

Previously, I use Array.filter to check if an array contains an item. To check if this is true, I use result.length > 0 for instance. Includes is a quicker and better suited alternative. If the value is present in the array, it returns true directly.

String split

Split is a common function used for splitting strings based on a delimiter. For example, you can use it to separate the first and last name from a full name string with a space delimiter in between: "John Doe".

I find it particularly useful when dealing with dates even when using excellent libraries such as MomentJS or LuxonJS.

String substring

Yet another useful function that is used in a lot of string manipulations. You often want only a part of a string, you can achieve it using substring - the start position and the length.

String includes

Yet another highly useful function to check whether a part of characters is found in a string. For instance, you might want to see if an email contains the first name of the user.