Date Object in Javascript

Let’s see a few points about when to use a date object, and why use this object vs. a date library? The simplest and quickest way to get date in Javascript - console.log(Date.now()); // 1549025852124 Date.now() does not create an object, and is really fast. But, it is also useless if you want to do anything with the date. Use date objects in numerous ways to format dates, set dates, consider time zones, and for date manipulations. ...

Arguments Object in Javascript

Arguments object enables you to treat function arguments with respect.. and the developers with compassion. How do you pass arguments to a function? In its most primitive form: function getSum(a, b) { return a + b; } This is perfectly ok in an ideal world where Buddha walks the earth. In the real world populated by Agent Smith and his compadres, one has to be more defensive with function arguments. One of the ways of doing that is by using arguments. ...

Non-enumerable Properties in Javascript

When you define a prototype for an object you run the risk of counting that as an object property. Use non-enumerable to avoid doing that. Consider this - Object.prototype.colors = () => { return; }; let fruits = ["apple", "orange"]; console.log("fruits length: ", fruits.length); for (fruit in fruits) console.log(fruit); // 0 1 colors Even though the array has two elements, it ends up showing the index 0, 1 and ‘colors’ which is the defined prototype. This can be avoided in two ways - ...

Create your Own Javascript Playground

I write quite a bit of Javascript on a day to day basis. But, I have a poor memory and keep forgetting things. Things like how I should be using a certain function the right way. This is where web applications like https://jscomplete.com/playground help. But it is not a pleasant experience to have blocks of incomplete code lying around in jscomplete, and keep executing few other parts of the program. I use the following tools to quickly know how to write a piece of functionality, or quickly test parts of code/components. ...

Context of This in Javascript

I have often seen how people talk and write about this being all complex and such. But frankly, I don’t understand the pain. All it takes for me to understand is 4-5 iterations of using this in a different functions, classes, modules and so forth, messing up a bit - each time, and trying to do that over & over until I get the right context with the right this. ...

5 Ways to Catch Common Javascript Bugs

Javascript is a God-send if you are trying to get things done - quickly. It can run back-end servers, is invaluable for the front-end, and you will never thank enough when you on the way to complete a project that could have taken 10x the time/effort back in the day. All that comes at a cost. Types seem to be the most blamed for errors, but they could range from anything from bracket mismatches to just ’typing’ issues. ...

Use Google Recaptcha in Vue Forms

Google recaptcha is quite a powerful tool in reducing spam through our contact forms. Once included in our forms, reCaptcha automatically manages suspicious activity, prompts additional ‘bot vs. human checks’, or is completely transparent to the overall flow - depending on who is interacting with the site. Including reCaptcha in Vue is quite simple. 1. Install vue-recaptcha-v3 Install the NPM package called ‘vue-recaptcha-v3’. This will enable us to just plug and play recaptcha rather than including the 4-5 lines of code provided by Google. ...

Show FAQs in Vue - The Simple Way

In one of the recent projects there was this requirement to create a FAQ page for a small application. The change was not planned and came up during the final stages of the project. I took it in the same spirit as what I do for a home page. I did not plan to use SSR or a static-site generator, but rather leaned back on the excellent ‘prerender-spa-plugin’. prerender-spa-plugin will take care of pre-rendering the content, and I don’t need to worry on the performance or the SEO side of things. ...

Open Google Map URL on Button Click in Vue

Recently I had to incorporate Google map functionality on list of contact cards. I though it would be interesting to outline a decision map of what went into the design and development. My first thought for the map functionality was to embed the map alongside the contact. This was quickly discounted since we had too many contacts on the same page (with a possibility of infinite scroll in the future). Although I could lazily load maps, one too many map requests did not make a lot of sense when all the user wanted did was to search and open a specific contact. ...

Change Function Scope in Javascript

Javascript has lexical scope - the variable values are available and valid based on their position. The same is applicable to functions. At the same time declarations are brought to the top of their block at compile time. In other words, they are hoisted at the time of compilation, and hence variables and functions are available to statements accessing them before the actual declaration. Consider the following function - getSum(2, 3); function getSum(x, y) { var sum = x + y; console.log("sum function: ", sum); // 5 return sum; } Even though the function is declared after the actual call, the above piece of code works without issues. ...