Javascript
Simple type conversion in arrays using map
· ☕ 1 min read
Type conversion is a big deal, but not anymore? Just use maps against a given array and them conversion shortcuts to simplify type conversion in Javascript. Consider the below array - 1 const nums = [1, 3, 5, 7, 9]; We can convert the type to string using a map.

Name thy variables while destructuring arguments
· ☕ 1 min read
Destructuring is useful but what if you have conflicts with the variable names provided by the object being imported? Consider the following example where we destructure arguments of a function. 1 2 3 4 5 6 7 8 const getSum = nums => { let { x, y } = { .

A simple way to specify required function arguments
· ☕ 1 min read
Specifying one or more arguments as required is one of the common patterns in Javascript. We will see one of the ways in which we can (arguably) make it more simple. Consider the function below - 1 2 3 4 5 6 function getSum(x, y) { return x + y; } console.

Simplify Conditionals Using Objects in Javascript
· ☕ 1 min read
Conditional statements and objects - are we talking about the same thing here? Do they have anything in common? Not quite similar, but you could simplify your conditional statements using objects. Consider the below tax rate calculation logic - 1 2 3 4 5 6 7 8 9 10 11 12 13 14 const incomeLevel = "high"; let taxRate; if (incomeLevel == "very low") { taxRate = income * 0.

Map vs. Object in Javascript
· ☕ 2 min read
When should use maps? What are maps anyway? Using an object is cool. We do it all the time - 1 const earth = { name: "earth", position: 3 }; What if we do not have all props at once, but rather incrementally assign props. 1 2 3 4 5 6 7 8 9 10 const earth = { name: "earth", position: 3 }; for (ele in earth) { console.

Break and Continue in Javascript
· ☕ 2 min read
Javascript does not have goto - thankfully. But it has continue and break that more or less perform the same function. If for any reason you want to jump out of a loop, here’s how you can do that - 1 2 3 4 for (let i = 0; i < 10; i++) { console.

Parallelize Work with Workers in Javascript
· ☕ 2 min read
Use workers when doing lot of work that can be spawned off in another “thread”. How do you open parallel work threads today? Possibly using async functions? Async functions do not block the main thread but allow processing to take place in parallel. Well, almost in parallel - see how event loops work.

Cloning objects in Javascript
· ☕ 3 min read
Cloning objects is easier than ever before, but you have to remember a few nuances. We will discuss about two ways to clone/copy an object - Deep copy: copy object so that any subsequent changes to new object does not reflect in older object Shallow copy: copy props and child objects of an object, but the new object keeps pointing to the original Both have their uses.

Function returns undefined unless specified otherwise
· ☕ 1 min read
Why is my function returning undefined when I have not mentioned that anywhere. The crux of the issue is in the topic header. All functions have to return something. If nothing is specified, they return undefined. 1 2 3 4 5 6 7 function doSomething() { // do something } const something = doSomething(); console.

Why use JSON.stringify() in debugging statements?
· ☕ 2 min read
Does JSON.stringify() have anything to do in debugging statements today? There were many Javascript-like systems that did not have the same tooling or debugging methods. I do not quite remember having browsers or ‘node’ print out the entire object back in the day - I may be wrong. So it was that I had to use console quite a bit, and what better way to print objects other than converting them to strings?

Use Strict in Inherited Javascript Code
· ☕ 2 min read
You should use strict mode everywhere, but what about inherited code? Typically, you designate strict mode at the beginning of the module - 1 "use strict"; But the module and its friends may have millions of lines of code that you did not write and do not have time to fix.

Assign values vs. push in Javascript arrays
· ☕ 1 min read
There is this myth that assigning array elements is better performant than push. I believe this originated from Javascript of yore, but is not relevant anymore. In fact, today push may be almost equal to or edge out assignment of array elements using keys (depending on runtime engine that is).

Nullify prop value vs deleting key in an object
· ☕ 3 min read
It is common advise to nullify a prop value within an object rather than deleting the prop. Should you prefer one over the other? Especially, in performance intensive applications. Delete object prop What do you do when you do not need a key-value pair in an object? Why, we simply delete it, of course.