13 Advanced JavaScript Tricks Any Developer Should Know

0
470

This blog post is a 13-part series and NCR Report Cards will introduce you to 13 advanced JavaScript tricks that every developer should know. It may seem daunting at first. But after reading these posts, you will be able to tackle any JavaScript challenge with ease!

Programming, Html, Css, Javascript, Php

Working of keywords:

The first trick we will discuss is understanding how the keyword, this , works. This may be a simple concept to some. But I find that it can get very confusing for developers who are just starting out with JavaScript and programming in general. It’s important to understand because using this incorrectly has caused many bugs within my code! Let me explain by posing a question: What does this mean? If you answered something along the lines of “it refers to what object or value,” then you’re right! But, there’s more…

In Javascript, functions have access to an additional variable called arguments . Think of these as variables which store all values passed into a function.

Sometimes it can be helpful to pass a value into the function, but not explicitly declare that variable. In this case, we can use arguments in order to do so! This is where things get interesting.

Use String.prototype to merge with other strings

JavaScript is all about manipulating text. So it should come as no surprise that there are many ways of merging one string into another.

There might be a case where you need multiple strings merged together. But don’t know the length beforehand for instance if you want a tooltip or FAQ title consisting of two pieces of information from different variables. In such cases, JavaScript’s built-in concatenation methods can handle this task easily by appending each piece onto an existing string. But this can become verbose and confusing. With just a few lines using some native browser API objects we can do much better! Take a look at the code below which shows how to do this in 13 lines of code. It might be useful if you want to save some space. It uses String.prototype and the Array object’s join method:

Use String.prototype to format numbers

JavaScript’s built-in formatting for numbers is very simple. It can be used by passing a number as the first parameter of the Number function, with any other parameters depending on whether you want them to round up or down (with handy options such as those available in PHP). The second trick we’ll show how to use some native browser objects like Array.prototype.join and apply it specifically for formatting integers:

Use Array.forEach() instead of loops

Array .forEach() provides an easy way of iterating over arrays without having too much code repetition that we would normally see if we were just using normal JavaScript syntax, something that potentially results in bugs later on.

It’s also more readable and easier to follow than standard for loops. This code example shows how we can use the built-in Array .forEach() function which was introduced in ECMAScript 2015 (ES2015) instead of a regular loop.

Use String.prototype to get random numbers from arrays

One last trick with strings is that you can actually create random number generators by using them as part of an array or object literal. So even though these aren’t truly “random”, they do have some pretty impressive results! Check out this snippet below where it uses Number(). But could easily be any constructor if you wanted something different such as Date()

Some of these may require using  the chrome browser or firefox debugger tools. If you are not already familiar with them then I recommend that you check it out now as they can be very useful for debugging your code and finding problems such as memory leaks which we will discuss later on. Let’s get started:

Set element focus/blur

One way to do this would be by adding an id attribute to the element you want to focus on and then checking for its existence using JavaScript. We can use a method which takes a string as an argument representing the id of your chosen element.

Get the index of an element in a jQuery object

You can easily get the position of your selected elements using JavaScript. The following line will return 0 if nothing is selected (or there are no matching elements). If they do exist, it will return their position starting from zero.

Get the parent node for any given element

This example shows how you might use this function to find out which file input field was last clicked on before calling another function with that information. Using setTimeout allows us to give priority to certain events by ensuring that code executes once other functions have completed their tasks first.

Retrieve data from one or more inputs at runtime

Here’s some simple code showing how we could collect all text values entered into an input field. We are using the HTMLInputElement type here to represent any element that submits text. But if you wanted to collect other types of data then it’s likely that there will be a corresponding jQuery object for each individual element which you can use instead. We could then later use that data to do whatever it is we want, such as using the value of the first input field in our form for a specific purpose.

LEAVE A REPLY

Please enter your comment!
Please enter your name here