Javascript
Sleep Function in Javascript
· ☕ 1 min read
Sleep holds off your function execution for specified time. This is super useful whenever you want to hold off something for ‘x’ time. Here are two ways in which you can get your program to sleep for a while. setTimeOut 1 2 3 4 5 6 7 8 9 10 11 12 13 function sleep() { console.

Template Literals in Javascript
· ☕ 1 min read
Template literals provide an easily readable way to embed expressions within string literals. For example - 1 2 3 const thirdSmartPlanet = "earth"; const fact = `${thirdSmartPlanet}is flat`; console.log("fact: ", fact); // fact: earth is flat Earlier, you had to combine strings and variable like so -

Destructuring Variable Assignments in Javascript
· ☕ 3 min read
Destructuring assignment feature provides an easy, readable way to handle object and array assignments. ES6 introduced assignment destructuring, which provides a clean and more readable way to assign variables. The old way - 1 2 3 4 const fruits = { apple: { color: "red" }, orange: { color: "orange" } }; const appleTheFruit = fruits["apple"]; console.

Arrow Functions and `this` in Javascript
· ☕ 3 min read
Arrow functions have made our life much much easier. You can now refer to this without fearing the bogeyman. How did we write functions in the dark ages? Here’s a typical function. 1 2 3 4 5 6 function getSum(i, j) { // or const getSum = function(i,j) { return i + j; } console.

MeteorJS - Getting Started
· ☕ 3 min read
Meteor is quite an easy Javascript framework to pick up and I have been fascinated getting back to where I left off a couple of years ago. There are quite a few things that you could do in the first week itself, and could even target to complete real-world projects.

Generate Random Strings in Javascript
· ☕ 1 min read
Generate random strings in multiple ways in Javascript. Use crypto The quickest and easiest way to generate random strings. 1 2 3 const crypto = require("crypto"); var txt = crypto.randomBytes(10).toString("hex"); console.log("txt: ", txt); // a3f4ac84da8da6bf172b Crypto library is available in node. On the client side, use window.crypto.

Handle Exceptions in Javascript
· ☕ 3 min read
Get a quick-start on using try/catch to handle errors and exceptions. Consider the below example that expects name to be provided to the program. If name is provided you will go ahead and store record. 1 2 3 4 5 let name = ""; if (name) { // store record } The big problem with the above code is absence of any kind of feedback to the caller (or user).

Format Number as Currency in Javascript
· ☕ 1 min read
Use Intl or the number prototype to format number to any specified currency. 1 2 3 4 5 6 const money = 420000; const moneyFormatted = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", }).format(money); console.log(moneyFormatted); // $420,000.00 Javascript falls back on runtime locale if you specify an unavailable locale.

Check if property exists in Javascript
· ☕ 3 min read
Quickly check whether a property exists against an object. There is more than one way to check whether a given object has a prop - the choice if often convenience rather than performance of the said method. Let’s see the various methods by taking this example of a simple object.

Find and Replace Substring in Javascript
· ☕ 1 min read
Find and replace substring within a string using replace. If you know the exact substring use it directly in replace and be at peace. 1 2 3 let sample = "Lorem ipsum dolor sit amet ..."; sample = sample.replace("ipsum", "gypsum"); console.log("sample: ", sample); // Lorem gypsum dolor sit amet .

Right Way to Use Console for Devtools
· ☕ 3 min read
Here are some effective ways of inspecting variables used in your code using browser dev tools to your advantage. This is what we typically do in the code to know what exactly is happening with our variables. 1 2 var name = "Mr. Anderson"; console.log('before loop" + name); This will have the following beautiful output in browser console.

Form Data Validation in Client and Server
· ☕ 2 min read
I am using Vue quite a bit and Vuelidate/ VeeValidate have spoilt me with options. The client-side validations are quite neat and easy to do. All you need is a couple of lines of code, some rules in the script section and you are good to go. However, you should not stop at this.

Filter Duplicate Attributes in Array of Objects in Javascript
· ☕ 1 min read
Catch duplicate attributes within an array of objects and retrieve unique elements. Array/object in question - 1 2 3 4 5 6 const students = [ { name: "Rama", grade: 10 }, { name: "Kris", grade: 5 }, { name: "Pete", grade: 7 }, { name: "Mo", grade: 5 } ]; We want to get unique grades from the array.