Find Difference Between Arrays in Javascript
· ☕ 1 min read
Use set and filter to find the difference between two arrays. 1 2 3 4 let fruits1 = ["apple", "orange", "grapes"]; let fruits2 = ["banana", "mango", "apple"]; let difFruits = [...new Set(fruits1)].filter((item) => !fruits2.includes(item)); console.log(difFruits); // [ 'orange', 'grapes' ] Similarly if you want to find the elements common in both arrays, you could employ a slightly different strategy.

Initiate Arrays with Data in Javascript
· ☕ 1 min read
Create arrays and initiate data in a single statement. Traditionally you would do the below to initiate the array and fill it with values - 1 let planets = ["mercury", "venus", "earth"]; This is a-ok if you know the values, but what if you want array to have fixed elements with pre-defaulted values?

Empty and reset objects in Javascript
· ☕ 2 min read
Is emptying objects akin to destroying them? Is destroying of objects easier than creating them? Let’s find out. Initiate an object. 1 2 let apple = { name: "apple", color: "red" }; console.log("apple: ", apple); // { name: 'apple', color: 'red' } Reset value 1 2 3 // the easy way to reset apple apple = {}; console.

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 .