Skip to content
Autana Business Partners

Javascript good practices; Variables

Published on 2 min read

autanabp

Let's look at a few good practices in relation to Javascript variables, many of which we apply without knowing that they are "good practices".

Avoid the use of global variables: One of the biggest problems of global variables is that they can be overwritten by other scripts, since in most cases it will always be better to use a local variable and learn how to use closures (or scopes) well.

Statements always above: It is not very difficult to follow this practice, since most of us apply this, unaware that it is a good practice. Declaring the variables always above the whole of the function or script we will get a cleaner code, since we will always have a place to look for local variables, it is also easier to avoid unwanted (implicit) global variables, as well as reduce the possibility of declaring the same variable several times (this rule also applies to variables in loops).

Initialize variables: It is always convenient to initialize variables when they are declared. This will give cleaner code, will provide us with a single site in which we will initialize everything and most importantly, it will avoid undefined values.

Do not declare Objects of type Number, String or Boolean: Always treat numbers strings and Booleans as primitives. not as Objects. Declaring these types as objects reduces execution speed and gives unwanted side effects.

var name = 'Ochando';

var name2 = new String('Ochando');

(name === name2) false, since name is a string and name2 is an object

var nickname = new String('Barran');

var nickname2 = new String('Barran');

(nickname === nickname2) fake

Avoid "New" as much as possible: In reference to the above, in most cases it will be better to declare the variables as primitive rather than as objects.

var a = {}; Instead of new Object()

var b = " "; Instead of new String()

var c = 0; Instead of new Number()

var d = false; Instead of new Boolean()

var e = []; Instead of new Array()

var f = /()/; Instead of new RegExp()

var g = function(){}; Instead of new Function()

Updated on

Tags

← Back to the blog

  • Data Management

    Optimization of energy consumption

    Discover how the optimization of energy consumption reduces costs and environmental impact. Learn to analyze data, simulate scenarios, and implement energy-saving strategies.

    Read article

  • Analytics

    Talent retention in the digital age: how analytics can help

    Learn how talent retention in the digital age benefits from analytics. Discover how data helps reduce turnover and retain your top employees.

    Read article

  • Success Stories

    Proyect Sourcing Optimization

    In the digital age, Project Sourcing Optimization has become essential for companies aiming to enhance their purchasing processes. By integrating data-driven decision-making and…

    Read article

Javascript good practices; Variables - Autana Business Partners