Javascript
Finally block has the final say
· ☕ 1 min read
Finally overrides any return statement from the try/catch. We write try/catch/finally for error handling in Javascript - 1 2 3 4 5 6 7 8 9 10 11 function tryTryTry() { try { return 4; } catch (e) { console.log(e); } finally { return 42; } } console.log(tryTryTry()); //42 The function returns 42 and not 4.

Associative arrays in Javascript
· ☕ 1 min read
tldr; There are no associative arrays in Javascript. But there is indeed a credible alternative. In PHP, we can do the following - 1 2 3 4 var fruits={}; fruits[3]="apple"; fruits[2]="orange"; fruits[1]="pear"; Javascript does not have associative arrays. All you can do is - 1 const fruits = ["apple", "orange", "pear"]; Yes, you can define props for the array itself, but that is an altogether different thing.

Allow a function call once and only once
· ☕ 1 min read
Allow a function to be called once and only once. We have previously seen an example where debounce can be used to call a function once and can ignore subsequent calls. There is a shorthand way to allow a single function call that if you are not interested in the debounce part of the problem.

Using switch with number ranges
· ☕ 1 min read
You can easily use switch to check whether a number is in specified range. We have seen the advantages of using switch instead of if/else. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 const num = 1; switch (num) { case 1: console.log("one"); break; case 2: console.

Negative number index in Arrays
· ☕ 2 min read
Negative number indexes do not countdown in an array in Javascript. There was this forum post that asked about why negative numbers do not work “as expected” in an array. The logic of the question is - 1 2 3 const nums = [1, 3, 5]; console.log(nums[-1]); // should have been 5, but returns undefined I fondly remembered my good ol’ days with Python, and then thought about two things -

Check if type of value is array
· ☕ 2 min read
Check whether a given value is an array before subjecting it to array methods. Loose typing can be a pain when your methods are being called from other methods / external modules. It is a good idea to check for valid types to avoid throwing runtime errors that are difficult to understand or parse.

Get a random value from array
· ☕ 1 min read
Get a random unique value from a specified array. You can use one of the hundred ways to get values at a specific index, or iterate over an array and get values in sequence. 1 2 3 4 5 const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9]; console.

Shorthand for multiple 'or' condition checks
· ☕ 1 min read
Use this shortcut to do multiple checks in one go. You have seen an or condition a billion times before, haven’t you? 1 2 3 4 5 6 const fruit = "apple"; let shape; if (fruit == "apple" || fruit == "orange" || fruit == "water melon") { shape = "round"; } console.

Shortcut to initialize objects
· ☕ 1 min read
Here’s a quick and more readable way to initialize objects from other variables. But, first - how do you normally define objects? 1 const nums = {}; I mean.. initialize objects - from other variables. 1 2 3 4 5 let x = 2; let y = 4; const nums = { x: x, y: y }; console.

Use Array Fill to Create Magic
· ☕ 1 min read
How to use Array.fill() to your fill. We have seen examples of how to use Array.fill() to quickly fill data in an array. But, can we do more? Yes..! You can generate array with sequential numbers. Use Array.fill() to line up your number array in sequence. 1 2 3 const nums = new Array(10).

Use Bubble Sort to Manually Sort an Array
· ☕ 2 min read
How can we use our beloved bubble sort in Javascript? We have seen examples of how to sort an array and sorting with multiple attributes. The easiest way is to sort an array is to just use Array.sort(). But that alone will not show you the complexity that you want to show in your program (for e.

Executing Functions Only Once in Javascript
· ☕ 1 min read
Let us see how to execute a specified function only once in the life time of the program. The Simple Way The easiest way is to use a global variable and remember the function execution. We can then ignore all future invocations. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 let saidHello = false; function sayHello() { console.

Reusable Debounce Function
· ☕ 2 min read
Debouncing a function gives us a way to wait for input to stop and only then trigger a set of actions. This is particularly useful when we have to call a function only when user input is complete (or there is a gap in typing that we can utilise for processing).