473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

5 Common JavaScript Development Mistakes To Avoid

JavaScript is a dynamic language of the web and it is extensively embraced by the developers across the globe. In fact the popularity of JavaScript contributed to its great community.

<>

Currently — new libraries, frameworks, and tools are frequently released to make JavaScript more powerful and very useful in the hands of competent developers whereas its established resources are constantly improved over time.

JavaScript is the number one language on the active repository called GitHub.

This same trend can also be seen on LiveEdu.tv, where 48,567 JavaScript related videos were created by the user base of enthusiastic learners and engineers who want to improve their career and skills.

Many developers take advantage of JavaScript in their front-end journey but, there are common JavaScript development mistakes made by most beginners and experienced JavaScript programmers. In this article — we will go through some of the common mistakes so you can avoid making them as well in your development process.


1. Memory Management

Memory management is crucial when it comes to development, and this is valid when working with JavaScript too. Many developers make the mistake of not thinking about memory management at all. This can lead them to several problematic instances since their application will use more memory than intended, and can also run the risk of crashing the entire system. Majority of these development were done without taking memory leaks into consideration.

JavaScript is garbage collected language hence it provides all the tools that are required to handle memory effectively.

“Roots” is used to handle garbage collections in JavaScript. Roots is a global variable that stores the references to different sections of the code. To attain some effectiveness with the roots variable, Sweep-and-Mark algorithm are used. You can read more about memory management in JavaScript by following a simple guide by Mozilla.


2. ==(comparison) and =(assignment)

Comparison and assignment operators are commonly confused by JavaScript developers. It can happen due to a typo even from an experienced developer or by a beginner who is yet to discover the difference between the two operator.

The assignment operator (“=”) is entirely different with the comparison operator(“==”). The assignment operator takes care of assigning a value to the variable whereas the comparison operator compares two values, and return 1 or 0. The real problem occurs because JavaScript lacks an error mechanism to detect this type of mistakes, so it wholly depends on the developer to manage this issue. Let’s see an example below;

Expand|Select|Wrap|Line Numbers
  1. var x = 23;
  2. if (x == 23){ do something; }
The above code will execute without any error. In the first statement, variable x will be assigned a value of 23. The second statement checks if the value of “x” is equal to 23. If it is true, the code under the condition will execute. Now, let’s take a look at the following line of code.

Expand|Select|Wrap|Line Numbers
  1. var x = 12;
  2. If (x = 2) { do something; }
Now as you can see in the above code, a typo has been made. The value of x is now assigned to 2 after it was initially assigned to 12 in the first statement. There would be no error messages for this type of mistakes as the statement if (x=2) evaluates to true or 1, it will lead to a cascade of changes in the behavior for the rest in the code. Not desirable at all!


3. ‘+’ symbol acts as concatenation and addition

Another common mistake among JavaScript development is not properly using the “+” operator. The behavior of “+” changes according to the context in which it is used. Let’s take a look at two different scenarios.

Scenario 1

Expand|Select|Wrap|Line Numbers
  1. var x = 23 + 8;
The above statement will return 31 and store it to the variable x. All fine.

Scenario 2

Expand|Select|Wrap|Line Numbers
  1. var x = 23 + “8”;
The above statement will also execute, but the end result that will be stored in x is 238. Strange? Actually, not!


‘+’ operator works with strings as well, and hence the result is 238 and not 31.

It’s better to avoid the mistake by being self-aware, or you can simply use the parseInt() function every time you want to do addition when a user input is involved.

Expand|Select|Wrap|Line Numbers
  1. var x = 23 + parseInt(“8”, 23);

4. Undefined vs Null

JavaScript works a little different when it comes to undefined and null. It handles variables and objects differently and assigns variable to “undefined”, and objects to “null” especially when they are initialized for the first time.

If you don’t take care of the default assignment, errors can easily creep into your program. To ensure that you neglect the issues, you need to check if the object is not null and the type of object not undefined. Also, undefined check should be done first as shown the code below.

Expand|Select|Wrap|Line Numbers
  1. if (typeof object! == “undefined” && object!== null){do something;}
The above line of code will work fine. However, the following line of code will throw an error.

Expand|Select|Wrap|Line Numbers
  1. if (object!== null && typeof object!==”undefined”){do something;}

5. Mistakes with block level scopes

The last mistake that we are going to discuss is how JavaScript developers fail to understand the block level scopes. Unlike other programming languages, block level scopes don't work similarly as they do in other programming languages. Let’s understand it with the help of an example.

Expand|Select|Wrap|Line Numbers
  1. for (var i=0; i< 5; i++) {
  2.     do something;
  3. }
  4. console.log(i);
  5.  
The above lines of code will return the value of i as 5. Surprised? Don’t be as this is how JavaScript works at the block level. It carries the last known value and the life of a variable is not limited to the scope of the for loop. The behavior is popularly known as variable hoisting.

So, how do you work with this kind of behavior? You can use let keyword to your advantage which is now been released with the ECMAScript 6. You can check more about it in the detailed JavaScript variable tutorial.


Final Thoughts

That’s it! If you note them and ensure that these mistakes don’t carry on to your JavaScript development, and can save a lot of time!

What type of common mistakes do you make when coding in JavaScript? Let us know by commenting below!
Feb 3 '17 #1
2 5798
Hi guys, any comment here? Each time I log in there is a lot of people around, but nobody is commenting. Any comment or suggestion will be highly appreciated...
Feb 6 '17 #2
Great tips! JavaScript devs will love it ;)
Feb 6 '17 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: SL at fecondcarcan dot com | last post by:
Hello, One of the great annoyances I have with JavaScript is the lack of a good consistent code library. Oh, there are a few code libraries scattered around for doing various things. The...
1
by: Yevgeny Konovalov | last post by:
Hi ALL! What do you suggest for Javascript development? I want to find a serious IDE for development of high complicated javascripts. Somthing like IDEA or Eclipse in java world. Thanx.
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
3
by: Dag Sunde | last post by:
What kind of dev environment do you guys recommend for developing Javascript code? I'm thinking more about the situation where one is working on libraries or larger bodies of code not related...
9
by: lorlarz | last post by:
I still have a question regarding the following code, in a commonly used routine. First, Here's the code in question: Function.prototype.bind = function(){ var fn = this, args =...
10
by: r035198x | last post by:
There are a lot of common C mistakes. I have put together the ones I consider to be the silliest made by C programmers. These are usually the source of the popular C programming tears and remembering...
0
by: anirudha d | last post by:
I am having user control multiple instances on same page. user control is embeded with javascript functionality. for eg. <script type="text/javascript" language="javascript"> var...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.