15 JavaScript Tips: best practices to simplify your code

Tags: javascript, algorithms, es6, productivity
Published: 2020-06-12 19:31:03

*This article was written by Ryan Thelin and was originally published on [Educative](https://www.educative.io/blog).*

So, you have a handle on writing JavaScript, but how do you go from practice to real problem solutions? How do you alter your JS code to make it simpler and easier to read? If you work on a team, it's important to write simple code. Since you're not working in a vacuum, your programs must be easy to follow by your team members. Everyone loves clean code! Learning how to make [JavaScript](https://www.educative.io/blog/javascript-es6-tutorial-a-complete-crash-course) as painless as possible is an invaluable skill sure to make you an office favorite.

Let's go over 15 best practice tips to help simplify your JavaScript and make it easily understandable by other developers. Here's what we'll cover. 

<small>

* [Declare and initialize your variables at the top](#1)
* [Build modular, specialized Functions](#2)
* [Recognize and remove duplicate code](#3)
* [Comment your code often](#4)
* [Beware of recursion overuse](#5)
* [Be efficient with DOM manipulations](#6)
* [Avoid global variables at all costs](#7)
* [Make use of shorthand notation (object literals)](#8)

<small>

* [Use Strict Mode to catch silent errors](#9)
* [Set default values](#10)
* [Use template literals to combine strings](#11)
* [Solve existence testing with `includes`](#12)
* [Shorten conditionals with false values](#13)
* [Sharing methods with inheritance](#14)
* [Write shorter loops with array methods](#15)
* [Wrapping up and resources](#wrap-up)

> *Looking for an advanced guide to simplifying your code? Check out our course [Simplifying JavaScript](https://www.educative.io/courses/simplifying-javascript-handy-guide) for hands-on practice.*


<br>

<a name="1"></a>

### 1. Declare and initialize your variables at the top

Nothing disrupts readability like a late declaration. Just as it's easier to take out all your tools before starting a job, it's simpler to declare all variables before getting into the nitty-gritty of your function. This gives easy access should we need to tweak any name or value later down the line.

While on the topic of variables, it's best practice to initialize your variables at the time of creation so you and your team can ensure none are left ``undefined``.

```javascript
<script>
var x = 5;
</script>
```

<br>

<a name="2"></a>

### 2. Build modular, specialized functions

No one function should have to do it all, for both efficiency and readability's sake. Instead, when designing functions, consider a single task that it should accomplish and write it to complete that task only. Name the function to match that task. 

This makes the code easier for others to read. The function will inherently be simpler and less extensive if only working toward one task. Further, it allows you and your team to lift this function to another program should you need it later. Even with just the passed variable names and function titles, we get a clearer understanding of where to find certain tasks in the second version below. 

```javascript
function table (columns, rows, item){
creates table and searches it for the passed item
}

// compared to

function createTable (columns, rows){
//creates table
}

function searchTable (table.length, item) {
//searches table for the passed item
}

```

<br>

<a name="3"></a>

### 3. Recognize and remove duplicate code

Similar to our last example, you should look out for instances in your code where you have identical lines of duplicate code. In cases like these, you should move the duplicated code into a function and call the function in all the instances that it was used before. This reduces visual clutter and aids in debugging later as the team can look at the one function rather than its multiple usage sections.

```javascript
<script>
var x = 5;
var y = 6;
var x = x*2
var y = y*2
</script>

<script>
var x = 5;
var y = 6;

function double (value){
return value*2;
}
double (x);
double(y);
</script>
```

<br>

<a name="4"></a>

### 4. Comment your code often

Comments are a great way to summarize a code fragment's purpose, saving your fellow developers the time it would take to determine it on their own. It also allows them to catch possible mistakes if the code does not complete the task it is commented to complete.  In general, it's best to leave one comment on every function. If you're unsure if you should leave a comment, just do it! It can always be deleted later if it's too much clutter. 

```javascript
//declares and initializes var x
<script>
var x = 5;
</script>
```

<br>

<a name="5"></a>

### 5. Beware of recursion overuse

Be mindful of nesting [recursive functions](https://www.educative.io/blog/recursion) too many levels. While capable of solving many problems, nesting is notoriously difficult to understand at a glance.
To avoid confusion, be mindful of where recursive functions can be pulled out of their nested place without significant runtime cost and do so whenever possible. If you have 3+ levels of nested functions, chances are your fellow developers will have a hard time following it.

```javascript

function1 (a,b){
  function2{
    function3{
	//this is too hard to follow and can likely be solved another way
    }
  }
}
</script>
```

<br>

<a name="6"></a>

### 6. Be efficient with DOM manipulations 

Accessing the DOM is essential for getting the most out of your program, but doing so repeatedly causes visual clutter and will slow down the program. Instead, access it once and cache it for later use in a variable. From then on, you can access that variable instead of the DOM directly. This process is visually cleaner and more efficient.

> *Note: When referencing DOM variables, it is standard practice to denote it using `$`.*

```javascript
function accountInfo(){
var email = $("#accounts").find(".email").val();
var accountNumber = $("#accounts").find(".accountNumber").val();
}

// Contents cached to variable "accounts"

function accountInfo(){ var $accounts = $("#accounts"); 
var name = $accounts.find(".email").val(); 
var age = $accounts.find(".accountNumber").val();
}
```

<br>

<a name="7"></a>

### 7. Avoid global variables at all costs

In JavaScript, variables have a scope in which they can be used, either global or local. 
These scopes decide where in your code these variables are defined or can be accessed.
Global variables can be defined anywhere in the program and exist outside of functions.
Local variables are only visible within the function it is defined.

If there are a local variable and a global variable with the same name, JavaScript will prioritize the local variable and ignore the global variable.
Global variables should still be avoided, as they can accidentally overwrite window variables resulting in errors. Further, having many global variables will slow down your program since they are not deleted until the window is closed, whereas local variables are deleted once the function is completed.

```javascript
<html>
      <script>
            var myVar = "my global variable"; // This variable is declared as global
            function localVariable( ) {
               var myVar = "my local variable";  // This is a locally declared variable
      </script>
   </body>
</html>
```



<br>

<a name="8"></a>

### 8. Make use of shorthand notation (Object Literals)

When designing objects or arrays in JavaScript, line space can be saved by opting for shorthand notation. This is accomplished by setting the properties or cells of an object or array during declaration rather than after. This saves you having to identify which object or array you're setting for on each line, making the section easier to read. While this may seem like a small change, it can save a lot of eye strain for your team as the objects and arrays get more complex.

**Longhand Object:**

```javascript
var computer = new Object();
    computer.caseColor = 'black';
    computer.brand = 'Dell';
    computer.value = 1200;
    computer.onSale = true;
```

**Shorthand Object:**

```javascript
var computer = {
    caseColor: 'black';
    brand: 'Dell';
    value: 1200;
    onSale: true;
}
```

**Shorthand Array:**

```javascript
var computerBrands = [
'Dell',
'Apple',
'Lenovo',
'HP',
'Toshiba',
'Sony'
];
```

<br>

<a name="9"></a>

### 9. Use Strict Mode to catch silent errors

JavaScript is a very forgiving language compared to other hardcoded languages like C++ and Java. While helpful for getting code to run without throwing errors, this leniency can lead to silent errors that pass without correction. This can also lead to inconsistent behavior if JavaScript can resolve the silent error in multiple ways.

To bypass this, opt into **Strict Mode**. This setting makes two major changes:
* Silent errors that would previously make it past the compiler now throw errors, allowing you to fine-tune your code before it reaches your team members.
* Fixes mistakes that prevent JavaScript from optimizing your code 
* JavaScript Strict Code programs often run faster than their "sloppy" counterparts

To opt into strict mode, add the line`` 'use strict'; `` either at the top of your script section (if you want the whole section to be strict) or before the desired function (if only certain sections should be strict).

<br>

<a name="10"></a>

### 10. Set default values
 
When creating objects, you can set default values for some or all properties of the object. Doing so ensures the values of each attribute are not ``undefined``. It also demonstrates what type of data is expected for that attribute. Additionally, by not setting default values for certain properties, you can communicate to your team that those values are not required for the object to function correctly.

```javascript
function logProperty({
    address = '111 11th Street, 11111', 
    unit, 	//optional
    landlord = 'Sara', 
    tenant = 'Raj', 
    rent = 500, 
})
```

Above, not all properties will have a unit number, but all will have the other four properties, which are populated with the data type expected. To demonstrate this, we leave `unit` blank.

<br>


> ## Keep the learning going. 
Learn how to write clean JavaScript code without scrubbing through videos or documentation. Pragmatic Programmers has compiled 51 real-world tips for JS coders. Educative's text-based courses are easy to skim and feature live coding environments - making learning quick and efficient. 
> [Simplifying JavaScript: A Handy Guide for Software Engineers
](https://www.educative.io/courses/simplifying-javascript-handy-guide) </b>

<a name="11"></a>

### 11. Use template literals to combine strings

Putting strings together is a pain, especially when combining strings and variables. To make this process simpler, you can use template literals (marked by backticks), which take both a string and variable.

```javascript
function greet(name) {
    return `Hi, ${name}`; //template literal
}
console.log(greet('Leo'));
```

Notice here that by using the template literal, we can log a greeting to any user based on the name passed to us, combining the string `Hi,` and the value of the passed variable ``name``. 
This code therefore prints:
``Hi, Leo``

<br>

<a name="12"></a>

### 12. Solve existence testing with ``includes``

Testing the existence of a value within an array is a common problem. Thankfully, JavaScript comes with a special array method, ``includes()``, which will return a Boolean if the array contains the searched value. Rather than searching the array, this method provides an efficient, easy-to-read solution.

```javascript
const sections = ['contact', 'shipping'];
 
function displayShipping(sections) {
    return sections.includes('shipping');
}
 
console.log(displayShipping(sections));
```

In the full [course](https://www.educative.io/courses/simplifying-javascript-handy-guide) from Pragmatic Programmers, you'll also learn how to use the ``indexOf()`` method to check a value and to find its index as well!

<br>

<a name="13"></a>

### 13. Shorten conditionals with falsy values

In JavaScript, there are many values that are equivalent to `false` across multiple types of variables. This includes: 
* the Boolean ``false``
* ``null``
* ``0``
* ``NaN`` (not a number)
* ``' '``
* ``" "``

In JavaScript, equivalent `==` means that the two objects share the same values, but they may not be the same type. Identical `===` means that the two objects are both the same type and same value.
But how is this useful?

Well, instead of creating separate variables to hold Booleans, you can instead use the above as default values to report ``false`` if nothing overwrites it. Consider this example, you need to check if a given employee has equipment training (``equipmentTraining``). This machine only requires the lowest level of training, the level of training is unimportant. 

As a result, our `if` statement checks if ``equipmentTraining`` still has a falsy value, the default ``' '``. If it does, the ``if`` statement executes and returns that the employee is not authorized. If ``equipmentTraining`` contains any string other than the default, it will have a truthy value and therefore not execute the `if` statement.

```javascript 
const employee = {
    name: 'Eric',
    equipmentTraining: '',
}
 
if (!employee.equipmentTraining) {
    console.log('Not authorized to operate machinery');
}
```

<br>

<a name="14"></a>

### 14.  Sharing methods with inheritance

[Inheritance](https://www.educative.io/blog/understanding-and-using-prototypal-inheritance-in-javascript) concerns a way of sharing properties or methods between classes. This is done using the ``super`` tag to allow the constructor in ``FlashCoupon`` to access the parent constructor in ``Coupon``. In doing so, you enhance the readability of your code by only defining the methods once (in the parent class). This makes your code to be more modular since inheritor classes can be specialized for a given task.

Notice that the top code box establishes a parent class, ``Coupon``, the properties and methods of which are shared to ``FlashCoupon`` as seen by ``FlashCoupon`` calling the ``getExpirationMessage`` method on its final line.

```javascript
class Coupon {
  constructor(price, expiration) {
    this.price = price;
    this.expiration = expiration || 'Two Weeks';
  }
  getExpirationMessage() {
    return `This offer expires in ${this.expiration}`;
  }
}
export default Coupon;
 
```

```javascript
import Coupon from './extend';
 
class FlashCoupon extends Coupon {
    constructor(price, expiration) {
        super(price);
        this.expiration = expiration || 'two hours';
    }
}
 
const flash = new FlashCoupon(10);
console.log(flash.getExpirationMessage());
```

<br>

<a name="15"></a>

### 15. Write shorter loops with array methods

For our final tip, we'll dive into the complexity of array optimization. Loops are a common way to create and populate arrays. However, they cause a lot of clutter and can be hard to read due to the number of lines required. Instead, you can use array methods to accomplish similar effects as ``for`` loops with only a fraction of the lines.
Take this ``for`` loop for example. 

```javascript
const prices = ['1.0', 'negotiable', '2.15'];
 
const formattedPrices = [];
for (let i = 0; i < prices.length; i++) {
    const price = parseFloat(prices[i]);
    if (price) {
        formattedPrices.push(price);
    }
}
console.log(formattedPrices);
```

Instead of the above code, we can get the same effect from three lines below by making use of the ``map`` method. This method creates an equivalently sized array with only the ``prices`` property. We then get float values of that price using the ``parseFloat``.

```javascript
const prices = ['1.0', '2.15'];
const formattedPrices = prices.map(price => parseFloat(price));
console.log(formattedPrices)
```

<br>

<a name="wrap-up"></a>

## Wrapping up and resources

The quest for cleaner code is an ongoing journey since best practices are constantly evolving with the language. This process is part of the fun of software development, and learning best practices demonstrates true mastery over a programming language.  

To get a great head start on learning more best practices and tricks to simplify your JavaScript code, check out Pragmatic Programmer's extensive, hands-on course [Simplifying JavaScript: A Handy Guide for Software Engineers](https://www.educative.io/courses/simplifying-javascript-handy-guide). By the end, you'll be a productive, modern JavaScript developer. 

<br>

### Other JavaScript resources
* [JavaScript ES6 Tutorial: A complete crash course on modern JS](https://www.educative.io/blog/javascript-es6-tutorial-a-complete-crash-course) 
* [Acing the JavaScript Interview: top questions explained
](https://www.educative.io/blog/acing-the-javascript-interview-top-questions-explained)