Javascript
Assigning default values while object destructuring
· ☕ 1 min read
Easily assign default values while destructuring objects. Consider below code- 1 2 3 4 const obj = { x: 9, y: 1 }; const { x, y } = obj; console.log(x, y); // 9 1 A typical destructuring of objects, this code assigns x and y variables to the x/y props of object obj.

Signify end of function with a return
· ☕ 2 min read
return terminates function whenever and wherever it is called. Consider this code block - 1 2 3 4 5 6 7 8 9 10 function getSum(x, y) { return x + y; let product = x * y; console.log(`sum is: ${x + y}, while product is ${product}`); } console.log(getSum(1, 5)); // 6 // no other console statements return will return the control to caller - no matter what state the function is in.

Three Invaluable shortcuts for type conversion in Javascript
· ☕ 1 min read
As Javascript developers we dream in type. Here are three quick and easy ways to convert from one type to the other in your Javascript code. Well, just kidding about the whole dream part. Most of us don’t quite care about types as much as we should. Anyhow, here are three quick ways to convert types -

Memoized Factorial Function in Javascript
· ☕ 1 min read
Get factorial of a given number using memoization techniques. Previously we have seen how memoization can be used in a reusable function to get all the advantages of memoization, without the complexity. Today, let us see one more practical example - get factorial of a given number. First, create a function to calculate factorial.

With statement in Javascript
· ☕ 2 min read
Avoid having to type your object notation multiple times using with statement. Consider the below code - 1 2 3 4 5 6 7 8 9 10 11 const earth = { name: "earth", satellite: "moon", position: 3 }; console.log("name:", earth["name"]); console.log("satellite:", earth["satellite"]); console.log("position:", earth["position"]); /* output name: earth satellite: moon position: 3 */ Typing “earth” everywhere can be quite laborious.

Prototype and property naming conflicts - shadowing issues in Javascript
· ☕ 2 min read
Be wary of prototype naming that can cause potential conflicts with one of the named objects. I have talked about using a non-emumerable property when defining a prototype, but what will happen if your prototype name conflicts with a prop name? You will loose quite a bit of hair - that’s what happens.

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.