Javascript
Return values from async functions
· ☕ 1 min read
How do you return anything from async functions? How do you receive what is returned from async functions? Previously we have seen how return works in Javascript. I typically write functions that return at least a true or false value. 1 2 3 4 5 6 7 8 9 10 11 12 function printThis(statement) { console.

Coercions can be counter-intuitive, use same-type comparisons
· ☕ 1 min read
Coercions are useful and almost intuitive, but beware of pitfalls. Consider below block of code - 1 const x = 42; Numbers greater than 0 and not undefined are generally considered truthy. So, following is understandable - 1 2 if (x) console.log("found the answer"); // found the answer But, you will be surprised if you do this -

Array `forEach` not iterating all elements
· ☕ 2 min read
I like Array.forEach() and use it whenever I need to iterate through elements. However one needs to be aware of its quirk. 1 2 3 const arr = new Array(5); arr[0] = 0; arr[4] = 4; A typical for outputs all elements including those not initialized yet (hold your breath for a moment on the initialized thing).

indexOf vs. find for Arrays
· ☕ 1 min read
Both indexOf() and find() are used for finding a pattern within an array. How are they different? indexOf requires the exact string to search within array. 1 2 3 4 const nums = [1, 3, 5, 7, 9]; console.log(nums.indexOf(3)); // 1 indexOf behaves very much like its string counterpart which helps to search a substring within a string.

Use variable value for object prop
· ☕ 2 min read
How do you set a variable as the prop of an object? Consider the below code - 1 const earth = { name: "earth" }; If we have a dynamic prop for earth, we can do - 1 2 3 4 5 6 let lifeExists = "life"; const earth = { name: "earth" }; earth[lifeExists] = true; console.

Create Null Object in Javascript
· ☕ 1 min read
Creating a null object is simple, but not in the way you may think. Consider the below code - 1 const earth = {}; earth is empty but not null. See - 1 2 3 4 5 6 7 8 9 10 11 console.log(Object.toString(earth)); // function Object() { [native code] } console.

Swap values between two variables
· ☕ 1 min read
Swapping variables is simple enough, but can it be any simpler? Consider the below code - 1 2 let x = 1; let y = 3; To swap values - 1 2 3 4 5 6 let z = x; x = y; y = z; console.

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.