4 Tricks to know about the Javascript language


With the advent of web development, developers have found themselves facing a very different environment from the one they used until some time before. That is, the execution environment of software executable on stand-alone machines, or rather, not of the server type. As I told you before, with the advent of web development technologies, one has had to deal with a stateless environment. Switching from one web page to another caused the loss of all objects managed by the previous page. Something that didn’t happen and still doesn’t happen with native executable software. Thanks to the Javascript language, many of these problems have been solved, and today, developers can count on a large variety of libraries, of the Javascript language, which are of enormous help to web development.

The Javascript language, by itself, is not a difficult or complicated language to learn. Indeed, it is a “C LIKE” style language and, consequently, very understandable even for a beginner. The approach to Javascript, in general, takes place without major problems. In any case, as with all languages, there are some tips and tricks that greatly facilitate development, making it simpler and more immediate to use. Today, in this guide, I’m going to show you 4 tricks you need to know about the Javascript language. To test the code described in this guide, you won’t need to create any web pages. You can simply take advantage of the online language interpreter “W3Schools”. Now, by clicking on this simple link , all you have to do is copy and paste, from time to time, the code proposed later in the guide. Once the page relating to the online Javascript editor has loaded, all you have to do is click on the “Try it Yourself” to access theJavascript editor directly. As a first step you will understand how to resize an array in Javascript.

W3Schools Javascript Emulator Online
W3Schools Javascript Emulator Online

1. How to resize an array

As you well know, arrays are not resizable. This applies not only to Javascript but to all programming languages. In fact, an array is a static and homogeneous data structure. This means that, during the execution of the code, it can neither be resized nor can it contain objects of a different nature. An Array can only contain data of the same type. For example, it can only contain strings, numbers, and other objects, but not strings and numbers, or anything else.

Therefore an array of 5 elements in Javascript like this:

const names = ['Mary', 'John', 'Mark', 'Philip', 'George'];

it cannot be resized, i.e. it cannot be deleted nor can other names be added. However, there is a trick, thanks to which, it is possible to shorten an array of this type. In fact, by setting the “length” property of an array to a smaller number of elements, compared to the one it contains, it is possible to resize it. For example, with the following code:

names.length = 2;

names”array will no longer contain 5 elements but 2. So if we print the contents of the “names” array, with this instruction: “console.log( names );”, the contents of the “names” array will now be: [‘Mary’, ‘John’].


Read more: “How to create website and PHP script automatically


2. Fetching only part of a Javascript object

In Javascript if we wanted to create an object containing properties, we should use a syntax like:

const names = {name: 'Steve', surname: 'Jobs'};

Normally to access the “name” and “surname” properties of the “names” object you should use a syntax like this:

names.name;

sometimes, however, it is only necessary to access a single property of an object. So, if we wanted to create a simple variable that contains only the content of the “name” property of the “names”object we would write a line of code like this:

const { name } = names ;

At this point, wanting to verify the content of the variable “name”, we can type this simple instruction “document.write( name );” which will print the value “Steve” on the screen. This very useful operation is called “Destructuring assignment“ or, destructuring of variables.

Destructuring in Javascript
Destructuring in Javascript

3. How to remove duplicate values from an array

In Javascript, to eliminate duplicate values from an array, you can use the “Set” object. “Set” Javascript objects that cannot contain duplicate values inside. So, to delete the duplicates from the “names” array you can use the “Set”command like below:

Get Unique element array
Get Unique element array

As you see, with a simple statement, you will be able to delete duplicates of any array without getting bogged down in complicated Javascript array cleanup routines. Often the source of countless bugs, annoying to find and fix.

4. How to evaluate expressions in a string

It’s not uncommon, especially when it comes to formatting strings, to value expressions directly within them. I’ll explain. As you well know, strings are also data types that cannot be modified except by creating another string which is the result of processing performed on the parent string. Yes, I know. Maybe you never noticed. But the strings you get, when processing the code, are all strings got from others. But the main string, i.e. the one you processed, has never changed. So every time you are about to format or create a string, containing values, at runtime, they are strings created by concatenating, and converting these values into strings, to other strings until forming the final one to be printed on the screen. I won’t deny you that an operation of this type is very boring and problematic. Furthermore, all the operation must be done at runtime with a huge waste of CPU and RAM resources.

In Javascript it is possible to format strings, enhancing the contents directly within it, saving time and obtaining cleaner code without. By the way, error-free. Let’s say right away that an operation like this:

const name = "Steve Jobs";
document.write ("Welcome"+name);

can be transformed into a simpler and more compact operation like this:

const name = "Steve Jobs";
document.write(`Welcome ${name}`);

Note that the string, in this case must be enclosed in single quotes ASCII code 96. And not the conventional ASCII code 39 single quotes.

As you see there is no data concatenation and casting operation. The string is unique and the “name”variable is valued directly within the string.

Evaluate Expression in Javascript
Evaluate Expression in Javascript

Read more: “How to make money online with TheMoneytizer


Network administrator and developer. After years of experience in the industry, I designed a MYSQL data management system in PHP without writing a single line of code. Find out more....

x