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. ...

Regular expressions in Javascript

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. Regular expressions can be used to match strings, identify specified patterns, and extract parts of strings. RegEx is a common enough function available in programming languages. They provide powerful ways to recognize patterns and test a given string efficiently and quickly. ...

Repeated function calls using an abstract pattern in Javascript

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. - function printStr(str) { console.log(str); } printStr("hello"); printStr("people"); /* hello people */ You can do it in a more concise way like so - (function(str) { console.log(str); return arguments.callee; // return function for next call })("hello")("world")("and")("people"); /* hello world and people */ Note that you are not returning a string, but a whole function and calling that function for the given arguments. In the statement, we use arguments to call the function that returns a function, and invoke the returned function on a specific argument thereon. ...