learn
Scope Chaining using Closures in Javascript
· ☕ 1 min read
You can easily chain methods along with variables to find a highly readable, and totally sensible scope chain. We have seen how closures can be used in Javascript before. Let us see some more practical usage scenarios. Consider the below closure - 1 2 3 4 5 6 7 8 9 function getProduct(x) { return function(y) { return y * x; }; } const twice = getProduct(2); console.

Function of Functions in Javascript
· ☕ 2 min read
Functions are objects in Javascript. So, we could use functions as objects within other functions. So, the below is valid - 1 2 3 4 5 6 7 8 9 10 11 12 function getProductPlus(x, y) { function factoredResult() { const factor = 2; //console.log("factored : ", x * y * factor); return x * y * factor; } return factoredResult; } let prod = getProductPlus(3, 5); console.

Boolean Primitives vs. Objects in Javascript
· ☕ 2 min read
We have come to love and respect the fact that (almost) everything is an object in Javascript and we can use all kinds of variables without giving a damn about their types. But, we also can understand that Javascript is trying to work its magic when we throw all caution to the wind.

Careful with key names of an object in Javascript
· ☕ 1 min read
An object’s key can be anything - even a reserved keyword. And, this may cause unexpected problems in some script engines. Consider - 1 2 3 4 5 const operations = { add: "add something", delete: "remove something", prototype: "do something" }; operations is valid. And, you can go ahead and use the props in any way you wish.

Sort Arrays on Multiple Attributes
· ☕ 2 min read
You can quite easily sort arrays on multiple attributes using a simple Array.sort(). Consider this array - 1 2 3 4 5 6 const students = [ { name: "Rama", grade: 10, class: 5 }, { name: "Kris", grade: 7, class: 1 }, { name: "Jan", grade: 8, class: 5 }, { name: "Kris", grade: 5, class: 7 } ]; Sorting based on grade is easy.

Merge Arrays / Objects using Spread Operator
· ☕ 1 min read
You can use the ... operator to perform black magic in Javascript. Consider the below example - 1 2 3 4 5 6 const arrNums = [1, 2, 3]; const arrMoreNums = [4, 5, 6]; const allNums = [...arrNums, ...arrMoreNums]; console.log("allNums: ", allNums); // [ 1, 2, 3, 4, 5, 6 ] This is much much more efficient than writing code to go through two loops and merging them.

Similar Methods Across Objects in Javascript
· ☕ 1 min read
How can you use methods of one object type against another object type? How can you use the feature to your advantage? In another post, we had discussed how Javascript can use call to invoke methods of a function in a different context. We can use this feature to our advantage.

Check if objects are empty in Javascript
· ☕ 2 min read
Check if a given object is empty without considering any custom props defined by prototype. Consider this sample object - 1 const earth = {}; Use Object.entries() or equivalent Object.entries gives us a short way to access entries in an array form. We could just check whether entries are empty to decide whether the parent object is empty.

Method of a Function in Javascript
· ☕ 2 min read
A function can have its own methods - strange but true. Why? Because everything in Javascript is an object and object can have methods. Consider - 1 2 3 4 5 6 function getSum(x, y) { return x + y; } console.log(getSum(1, 2)); //3 console.log(getSum.call(this, 1, 2)); //3 The first call to getSum is as expected.

Create Chainable Interfaces in Javascript
· ☕ 1 min read
Chainable interfaces make your code readable and, arguably, your life more enjoyable. Chainable interfaces are those series of methods that you see chained to an object. You can use them for objects where “relevant”. Commonly used functions It is common to see methods chained to one another. The chaining works from left to right, with results from the left-side of the function passed to the right in sequence.

Deleting Object Properties The Right Way in Javascript
· ☕ 1 min read
Do not set properties to null or empty, always use delete. Consider - 1 2 3 const earth = { name: "earth", position: 3, aweIndex: 42 }; console.log(earth); // { name: 'earth', position: 3, aweIndex: 42 } If you set any property to null - 1 2 3 earth["aweIndex"] = undefined; console.

Object vs. Prototype Properties in Javascript
· ☕ 2 min read
Check if a given object has a specified property. You can either use non-enumerable props or hasOwnProperty. Consider the below code - 1 2 3 4 Object.prototype.awesomeness = 42; const earth = {}; console.log(earth["awesomeness"]); // 42 We have defined a prototype that gets attached to any object. Therefore the prop defined by prototype is attached to the object as well.

Quickly Check if Array Elements Satisfy Given Criteria in Javascript
· ☕ 1 min read
Use Array.some() and Array.every() to check whether an array has elements that satisfy the given criteria. Consider the below array - 1 const nums = [3, 5, 6, 1, 2]; You can check whether the elements satisfy a specified criteria with below code - 1 2 console.