Javascript
Check whether array is a palindrome in Javascript
· ☕ 1 min read
Check whether a given array has a palindrome in Javascript. Below logic should be fast, if not the fastest. 1 2 3 4 5 6 7 8 9 10 11 console.log(checkPalindrome([1, 3, 5, 3, 1])); function checkPalindrome(arr) { const len = arr.length; if (len % 2 == 0) return false; for (let i = 0; i < len / 2; i++) { if (arr[i] !

Find average and median of arrays in Javascript
· ☕ 2 min read
Find average and median of a given array in Javascript. Problem We are given an array that is hopefully comprised of numbers. Find average and median. Get average Calculate sum, length and use them to get the average. 1 2 3 4 5 6 const arr = [3, 7, 2, 6, 5, 4, 9]; const sum = arr.

Find difference between dates in days in Javascript
· ☕ 2 min read
Easy and right ways to calculate difference between two dates in Javascript. Problem We are given two dates and we have to print the difference between those two dates in days. Since we are given two valid dates, half of our problem is already gone and we have not started yet.

Filter before you sort arrays in Javascript
· ☕ 1 min read
Array sorting is easy, but can yield unexpected results if you are not careful. Consider this example - 1 2 3 const nums = [5, 2, 7, 9]; console.log(nums.sort()); // [ 2, 5, 7, 9 ] All beautiful and compliant so far. But, what if Loki lurks his head?

Find where command is executed from in NPM
· ☕ 2 min read
It can get confusing if you have packages installed globally, and within project folder. Find out where the command originates from when you are using NPM. See below depending on what you are looking for. Info about a command that you put in the command line For e.g you do a -

A few useful commands in NPM
· ☕ 2 min read
npm is quite simple but immensely powerful for what we do as developers. At the heart of it, NPM just downloads stuff from its own registry on the Internet. But the underlying dependency management makes it an easy-to-use, super efficient tool. Here are some commands that I find useful in day-to-day work.

Package Managers in Javascript
· ☕ 6 min read
Since this blog existed and for many centuries before that, we had - “package managers”. These utilities have been quenching the thirst to build better applications and even better applications while standing on the shoulders of giants that grow ever bigger. Yes, they do all that despite their drab name.

Regular expressions in Javascript
· ☕ 4 min read
Regular expressions are patterns used to match and/or manipulate strings. This is a high-level overview of a few of the practical applications of a RegEx in Javascript. What is RegEx and what’s it doing in Javascript? A RegEx consists of simple and special characters that together form a language of its own.

Repeated function calls using an abstract pattern in Javascript
· ☕ 1 min read
Use this if you want to repeatedly call a function for known arguments, or in case you just need a IIFE. If you want a repeatedly called functions, you would typically - Call function in a loop Use recursion For e.g. - 1 2 3 4 5 6 7 8 9 10 11 function printStr(str) { console.

Flatten Those Arrays in Javascript
· ☕ 2 min read
Flatten arrays easily with a single command. Or, go further than that by using a map function while flattening arrays. Consider below example - 1 const nums = [[0, 2], [1, 3], [7, 9, 11]]; You can choose to iterate through all elements if you want to iterate through or access all numbers, or you could just use flat.

Dynamically Build Strings in Javascript
· ☕ 1 min read
You should dynamically build strings without using strings. How do you build strings when you need them conditionally built? For example, you need comma separated planets from an array of astronomical bodies. 1 2 3 4 5 6 7 8 9 10 11 12 const astroBodies = [ { name: "earth", type: "planet" }, { name: "moon", type: "satellite" }, { name: "mars", type: "planet" } ]; let planets = ""; astroBodies.

Require and Import in Javascript
· ☕ 3 min read
Require and import do the same thing differently. I never said to none about missin’ the good ol’ days. The days when Javascript was not golden, code could be simply included within HTML or a single file, and when smarty-pants use to go cyber without Javascript. Those were more of WTF-days.

Object Entries and Values in Javascript
· ☕ 3 min read
Object entries and values add to the rich toolset for managing objects within Javascript. As I never tire to say - (almost) everything is an object in Javascript. So, how do you access the props / values? It is not as if you are going to count them down - pfft.