December 11, 2018 9:00:00 AM EST

6 Common Mistakes in JavaScript

As with any language, JavaScript has several different aspects that can be difficult to learn. However, it is one of the core modern web application languages and most tech departments require a working knowledge. While it may seem like a simple language, it can be much more powerful and complex than you would first believe. It has many subtleties that can lead to problems for new developers. Here are some of the more common mistakes you might run into.

1. Using Block-Level Scope

While many other languages create a new scope for every code block, JavaScript does not. The variable in Java remains in scope even once the loop has completed and it retains its final value upon exiting. In other languages, it’s destroyed after the loop.

This is an often-missed quirk, particularly among new JavaScript developers. This is something to look into if you’re finding bugs in your code.

2. The “+” Symbol Means Addition and Concatenation

Not all languages work this way, but it does in the JavaScript library. This means you have to be careful about how you use the plus sign when you are writing statements. JavaScript developers use both strings and numbers to account for user input. This changes the output when you ignore that the user input can also be evaluated as a string. Convert the string value to an integer to avoid this issue.

3. Creating Leaks in Memory

Unfortunately in JavaScript, memory leaks can become an almost inevitable problem unless you make a point to code in an avoidance of them. Memory leaks are the result of object references that are unnecessarily sustained when they are no longer needed. This causes your machine to consume resources that aren’t needed. 

Two common things that can cause memory leaks when using the JavaScript library:

  • Dangling references to defunct objects
  • Circular references

Here is a good reference for identifying and fixing JavaScript leaks.

4. Inefficient DOM Manipulation

With JavaScript, it is not hard to manipulate the DOM, but there’s nothing that encourages you to do it efficiently. One example is the ability to add a series of DOM Elements one at a time. It’s an ineffective process likely to cause issues. However, you are able to add multiple DOM elements consecutively. Adding document fragments instead is an effective alternative which improves performance and productivity.

5. Equality Confusion

While it is one of the convenient aspects of JavaScript to coerce any value referenced in a boolean contest to a boolean value, it can also be confusing. Using [= = =] and [! = =] instead of [= =] and [! =] is best unless you are purposely going for type coercion.

6. Failing to Use “Strict Mode”

This is a way to enforce stricter error handling and parsing on your JavaScript code at runtime, and it makes it more secure. Though it might not be considered a mistake to not use it, it’s certainly considered best practices to use strict mode.

Some of the benefits include:

  • Easier debugging
  • Eliminates [this] coercion
  • Prohibits duplicate property names and parameter values
  • Prevents accidental globals
  • Shows errors on invalid usage of [delete]
  • Makes eval() safer

 

There are many things to know about JavaScript, the more you learn and understand the better your code will be. Soon you’ll be able to harness the full power of the language. For more information, reach connect with us here at Aspirant.

New call-to-action 

Phil Kossler
Phil Kossler

Phil is Aspirant's Technology Director with a focus on providing enterprise-grade solutions. His 30 years of experience have given him the opportunity to work across many markets, industries, and applications. Phil is leading a team of skilled web and mobile developers building strategic solutions for our customers.