A Few Common JavaScript Mistakes

The high-level programming languages become more and more modern, along with this rapid growth, they become more user-friendly to everyone (coders and programmers) and easer to implement.

However, in during working with these programming languages, obviously any programmer (coder) always confront some troubles they come with. This JavaScript article only discuss JavaScript - the most popular web programming language in client-side, along with some common mistakes.


Sampled by © JavaScriptBank.com

As technology advances, it's also becoming user friendly and easier to implement. Then how can JavaScript lag behind? Over the past few years, using JavaScript has become very easy. In early days, there was a need for developers to know about every browser to write JavaScript code compatible to them. But these days, there are many libraries such as Dojo, MooTools, jQuery and YUI, which allow a person without any knowledge of JavaScript, to work with the HTML code of a website.

No matter how much JavaScript has emerged, the ground rules of the script didn't change - maintainable, solid and secure JavaScript. So let's go through seven sins which you should not make to your code if you don't want to be screwed up while delivering, maintaining and handling the code to a third party.

One of the biggest problems with JavaScript was that it was browser specific. Many of the codes in JavaScript were written to be compatible to IE6 when it was the state-of-the-art browser. This is the reason that IE6 can't be removed completely by many companies and the newer versions of IE have to be compatible to support these IE6 specific applications. The role of JavaScript libraries is to make the development predictable so that developers can be relieved of the pressure of browser specific coding.

Listed here are some JavaScript common mistakes that should be kept in mind while developing JavaScript code:

1. DOM1 event handlers, global variables and functions: Problems that may arise because of these are illustrated below:
* If a variable or function name is defined, and any other include statement uses the same name, then the initial one will be overwritten.

Solution: use closures, var keyword, namespace your functions and variables to avoid this problem.

o The same concept applies to the event handlers if they are attached in the old on-Event way.

Solution: don't use on-Event handlers but the DOM level 2 event handlers.

o The rights of every script are same in HTML and a script can overwrite what other scripts have done before, if there is a need.

Solution: Wrapping of functions in more unique names can prevent this problem.

2. Expecting that everything is right and thus not testing: expectation that every element in the code is in the right format, the values which user enter are in the right format and every HTML element you are trying to enhance is available.

Solution: use typeof, indexof(), split, length, alerts() etc.

3. Use of wrong technology for the work: the maintenance becomes difficult and quality of code deteriorates.

Solution:

o Regardless of the display environment, essential content and mark-up should be written in HTML.
o CSS should be used to maintain "look and feel" elements.
o JavaScript should be used for interactions with user that goes beyond hover effect.

4. Although JavaScript is easy to use, but all the functionalities can't be implemented in it, so don't promise them.

Solution:

o JavaScript should not be used for any vital functionality of product.
o If you build anything with JavaScript, add it to document using DOM>
o JavaScript functionality should work with keyboard and mouse.

5. Making maintenance of code unnecessarily hard: because of work environment, deadlines and hiring practices.

Solution:

o Make form without using JavaScript.
o Keep maintenance of all HTML in the same document.

6. No documentation of code: Documentation is important because coding styles change with time and technology, hence the code will be easier to understand if documented.

Solution: use comments, especially with sections like start of classes and reusable functions, third party code, sections with dependencies.

7. Optimizing for people, but not for machines.

Solution: If you can do anything which can make the work of other machines easier, then do it.

Just by following these simple steps, you can make your code easier, cleaner and long lasting.

Language
Translate this page to English Translate this page to French Translate this page to Vietnamese

Recent articles
How to open a car sharing service
Vue developer as a vital part of every software team
Vue.js developers: hire them, use them and get ahead of the competition
3 Reasons Why Java is so Popular
Migrate to Angular: why and how you should do it
The Possible Working Methods of Python Ideology
JavaScript Research Paper: 6 Writing Tips to Craft a Masterpiece
Learning How to Make Use of New Marketing Trends
5 Important Elements of an E-commerce Website
How To Create A Successful Prototype For Your PCB


Top view articles
Top 10 Beautiful Christmas Countdown Timers
Adding JavaScript to WordPress Effectively with JavaScript Localization feature
65 Free JavaScript Photo Gallery Solutions
16 Free Code Syntax Highlighters by Javascript For Better Programming
Best Free Linux Web Programming Editors
Top 10 Best JavaScript eBooks that Beginners should Learn
Top 50 Most Addictive and Popular Facebook mini games
More 30 Excellent JavaScript/AJAX based Photo Galleries to Boost your Sites
Top 10 Free Web Chat box Plug-ins and Add-ons
The Ultimate JavaScript Tutorial in Web Design


Free JavaScript Tutorials & Articles
at www.JavaScriptBank.com