Javascript
Use homogenous arrays to maintain sanity
· ☕ 1 min read
Use values with same types in any array. Changing types (even for scenarios that you think are totally valid for the day) can rain down hell. Homogeneity of an array refers to using the same types of variables within an array. The following arrays are homogenous - 1 2 3 4 const fruits = ['apple', 'orange]; const nums = [0,1,2] const answers= [true, false]; The following arrays are not -

Is it really useful to cache array length in a for loop?
· ☕ 2 min read
It is a commonly recommended practice to cache array lengths while doing for loops and get better performance. Seriously, is that a thing though? Typical for loop In a typical for, you calculate length, iterate and you are done. 1 2 3 4 5 6 7 8 9 10 const arr = new Array(1e7).

Shortcuts to variable assignments in Javascript
· ☕ 2 min read
You don’t have to see assigning values to variables as a chore. There are exciting ways to do those assignments - here are some shorthand ways. Initialize to same value You can initialize variables like so - 1 2 3 let i = 0; let j = 0; let k = 0; Or, you can instead do -

Using strict mode in Javascript
· ☕ 2 min read
‘use strict’ is a rather useful statement to have at the beginning of all Javascript modules or functions. It ensures that all code in the module is executed in strict mode. The usage is simple enough. 1 2 3 4 "use strict"; // lot of code You can also enable strict mode to specific functions.

Avoid Scope Pollution in Javascript
· ☕ 1 min read
It is quite easy to pollute global scope by defining variables or functions that impact core functions. 1 2 3 4 const parseInt = val => console.log("int parsed: ", val); parseInt(1); // int parsed: 1 parseInt has transformed into a printer, thanks to the above code. The practice of “overriding” global scope is not useful and should be avoided like the plague (If you are born in the wrong century - “plague is to be avoided at all costs”).

Use Right Syntax for a Map
· ☕ 2 min read
Map on an array is simple enough - 1 2 3 const ran = ["1", "3", "5"].map(val => Number(val)); console.log("ran: ", ran); // ran: [ '1', '3', '5' ] map will specify one element at a time, which is passed to the callback function as val. Let’s say, we ignore the val.

Reusable Memoization Function
· ☕ 3 min read
Let us see how we could use a reusable function that can ‘memoize’ any given function. Memoization is a technique that can be used in long, recursive code to cache results from previous executions and speed up the overall process. Previously we have seen an overview memoization in JS with an example of generating Fibonacci series using such techniques.

Process one or multiple arguments within a function
· ☕ 2 min read
Let’s say you have to write a function that doubles a given number. You also find out that you may get one number (in which case double that number), or multiple numbers (double all numbers). The first thought that comes to mind is to - use different functions (or) check type of input and process differently Consider the below code -

Shuffle an array in Javascript
· ☕ 1 min read
Write a simple logic to shuffle a given array. Consider this array - 1 const nums = [1, 2, 3, 4, 5, 6]; You can shuffle this array by using the following code - 1 2 3 4 5 6 7 8 9 const nums = [1, 2, 3, 4, 5, 6]; nums.

Round-off numbers in Javascript
· ☕ 1 min read
How do you round-off a number in Javascript? The simple way - 1 2 console.log(Math.round("3.14")); // 3 console.log(Math.round("3.72")); // 4 Then, there are other interesting ways.. Round off to the nearest lower whole number Use Math.floor instead of round. 1 2 console.log(Math.floor("3.14")); // 3 console.log(Math.floor("3.72")); // 3 Round off to the nearest higher whole number Use ceil.

Memoization in Javascript
· ☕ 4 min read
What is memoization and where is it useful? Let us do a quick overview and dig into example code. Memoization is a technique that can be used in long, recursive code to cache results from previous executions and speed up the overall process. Consider a long repetitive operation where there is a good possibility that you will have the same arguments for the computation function more than once.

Global variables in async functions
· ☕ 2 min read
How do global variables impact async functions? Does hoisting have a role to play at all? As many beginners have found out the hard way - yes. 1 2 3 4 5 for (var i = 0; i < 5; i++) { setTimeout(function() { console.log(i); }, 500 * i); } What’s the output you’d expect?

Find max and min numbers in array
· ☕ 1 min read
What is the easiest way to find maximum and minimum of a given set of numbers? Well, that’s easy. 1 2 3 4 console.log(Math.max(1, 3, 5)); console.log(Math.min(1, 3, 5)); // 5 1 How about an array? The first thing that comes to mind.. 1 2 3 4 5 6 7 8 9 const nums = [1, 3, 5]; const max = nums.