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

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.