True comparison using Object.is()

Object.is() provides a true comparison for any object. Start using it today if you have missed out so far..! Consider the code below - console.log("abc" == "abc"); // true console.log(null == null); // true console.log(1 == 1); // true // break console.log(0 == false); // true console.log("" == false); // true While we are ok with the statements above break, the statements below can cause a problem. Typically this is overcome by using strict comparison - console.log("abc" === "abc"); // true console.log(null === null); // true console.log(1 === 1); // true console.log(0 === false); // false console.log("" === false); // false But, what about the only edge case that is so.. so.. so important - ...

Tagged Templates in Javascript

Tagged templates allow our beloved template literals to be taken to a whole new level. The typical use of a template literal is below - 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 - console.log("x is " + x + ", y is " + y); // x is 1, y is 5 Template literals are cool. Tagged templates take the above function in a different way - ...

Searching the Void in Javascript

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 - undefined = 0; console.log("undefined: ", undefined); The above code would return 0 instead of undefined a couple of years back. So, we did this instead - let planet; //.. a lot of code .. planet = void 0; console.log("planet: ", planet); // undefined void was a sure way to find the true undefined. ...

Check whether array is a palindrome in Javascript

Check whether a given array has a palindrome in Javascript. Below logic should be fast, if not the fastest. 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] !== arr[len - i - 1]) return false; } return true; } The code is simple - If given array length is even, it can’t be a palindrome Go through half the array, and find out whether array element is equal to the corresponding element on the “other side”. In other words, check whether first element is equal to last element, the second element is equal to the last but one element, and so on. ...

Find average and median of arrays in Javascript

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. const arr = [3, 7, 2, 6, 5, 4, 9]; const sum = arr.reduce((sum, val) => (sum += val)); const len = arr.length; console.log("average: ", sum / len); // 5.142857142857143 Get median Sort array. Median formula is different depending on whether length of array is odd or even. Array length = odd => median is the average of the two middle numbers Array length = odd => pick the middle number as median const arrSort = arr.sort(); const mid = Math.ceil(len / 2); const median = len % 2 == 0 ? (arrSort[mid] + arrSort[mid - 1]) / 2 : arrSort[mid - 1]; console.log("median: ", median); // 5 One important pre-requisite Although the above arrangement works, it is always a good idea to filter out our arrays for undesired elements! ...

Find difference between dates in days in Javascript

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. Get started Calculate number of days - simple and plain. const from = new Date(2019, 0, 1); const to = new Date(2020, 0, 1); console.log((to - from) / (1000 * 3600 * 24)); // 365 Date in Javascript is in millseconds since 1970. So the difference between two date objects is a big millisecond number. We just convert that to days. ...

Filter before you sort arrays in Javascript

Array sorting is easy, but can yield unexpected results if you are not careful. Consider this example - 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? const nums = [5, 2, undefined, 7, null, 9]; This problem occurs all the time since you are receiving arrays from external systems, from other libraries, or from code written by your “best friends” (quote intended). Arrays can now turn up results like this.. ...

Find where command is executed from in NPM

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 - vue create test1 .. and you see NPM run and create folder structure. But, you don’t know which this vue thing. ...

A few useful commands in NPM

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. Install / Uninstall Of course, I am joking. You would have seen this a billion times. npm install nodemon npm uninstall nodemon You could follow the shortcut notation ...

Package Managers in Javascript

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. Package managers address a single function - package code and manage dependencies so that they could be used as a whole. ...