Global Variables and Functions in Vue
· ☕ 2 min read
Vue provides a way to create global variables and functions using Vue.prototype. What do you normally do if you want to create a global variable in Vue? You don’t. Global variables have no place in today’s society You create variables in storage. That’s the purpose of storage You can say the exact same thing about functions.

Store your metadata for free in Javascript
· ☕ 2 min read
What if you want to play around with data, but also maintain metadata at the same time? In Javascript you don’t have to create other objects or work hard for that Nirvana that someone in the office was talking about. You just have to go ahead and store the darn metadata along with the object, and everyone is happy.

Equality Checks using NaN in Javascript
· ☕ 2 min read
It is easy to take NaN for granted. But remember - the ‘not a number’ notation is the only powerful object that does not equal itself. First the essentials - you use NaN whenever you do not have variable values in control but want to do a numerical operation on it.

Truly Constant Props in Javascript
· ☕ 2 min read
You can make mere variables and even an object immutable. But, how about a humble object prop? Making object props constant and immutable has practical usage. For e.g. consider this totally practical program to print out fruits and whether they are poisonous. 1 2 3 4 5 6 7 8 const eatable = { poisonous: false, printFruit: function(fruit) { return "name: " + fruit + " | poison: " + this.

Null and Undefined - Heroes who are equal to none in Javascript
· ☕ 1 min read
You have used null. You have used undefined. But, have you ever thought about what they mean to the world and to Javascript? The fact is quite simple - we can’t do a null equality check. 1 2 3 4 console.log(1 == null); // false console.log(0 == null); // false console.

Curry Using Bind in Javascript
· ☕ 1 min read
You can use bind to bind variables of a function, and execute the said function later to get the desired result. You have seen how how closures can be used in Javascript and a bit about currying functions. Here’s one more way of doing that. We can bind available variables for a function and execute function at a later time.

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.