Javascript
Access an array in reverse in Javascript
· ☕ 2 min read
Access an array in reverse. Consider the array in the code block below - 1 const nums = [1, 2, 3]; Accessing the array is easy enough. 1 2 3 4 console.log(nums[0]); console.log(nums[1]); console.log(nums[2]); /* 1 2 3 */ But, what if you want to access the array from the last element?

Optional arguments using spread while calling functions
· ☕ 1 min read
How about using the spread operator for handling optional arguments, or for null arguments? Consider this code - 1 2 3 function sum(x, y, z) { return x + y + z; } We can choose to pass ‘z’ or not, but ignoring ‘y’ returns an error.

Console.log function for easier debugging
· ☕ 2 min read
Encapsulate a console.log() in a function and use it anywhere - even within the most complex expressions. The most typical console.log - 1 2 console.log("hello world"); // hello world Change it to - 1 2 3 4 5 6 function logThis(expr) { console.log(expr); return expr; } logThis("hello world"); // hello Why the circus?

True comparison using Object.is()
· ☕ 2 min read
Object.is() provides a true comparison for any object. Start using it today if you have missed out so far..! Consider the code below - 1 2 3 4 5 6 console.log("abc" == "abc"); // true console.log(null == null); // true console.log(1 == 1); // true // break console.log(0 == false); // true console.

Tagged Templates in Javascript
· ☕ 2 min read
Tagged templates allow our beloved template literals to be taken to a whole new level. The typical use of a template literal is below - 1 2 3 4 5 let x = 1; let y = 5; console.log(`x is ${x}, y is ${y}`); // x is 1, y is 5 Template literals allow us to avoid stupid statements like this -

Searching the Void in Javascript
· ☕ 2 min read
What is void and does it have a role in modern Javascript? The unary void operator is rather strange and has quirky use cases. Or, should I say ‘had quirky use cases’. Primarily, void was used to check the true undefined. Consider the below code - 1 2 undefined = 0; console.

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.