JavaScript tips and tricks for freshers

As a follow-up to Javascript shortcuts for loops, conditions and return functions, this week, we’ll review Javascript tips and tricks. Once you’ve reviewed the list, be sure to let me know what little tips you’ve come across too!

General javascript tips and tricks that everyone should follow while coding.

javascript tips and tricks
javascript tips and tricks

Javascript is a language that can be use with HTML and that is independent of any specific programming language. With node js, jquery bbq and many more js framework we can build a stand alone beautiful web application even without any language like PHP, JAVA, PYTHON etc.

  1. Always code peacefully
  2. Don’t be hasty
  3. Code with proper indent and spaces
  4. Don’t put extra blank lines
  5. Only comment when required
  6. Try to follow naming convention for coding standards. It looks beautiful.
  7. Always console.log(). Don’t use alert.

JavaScript utilizes two different kinds of equality operators: === | !== and == | != It is considered best practice to always use the former set when comparing.

“If two operands are of the same type and value, then === produces true and !== produces false.” – JavaScript: The Good Parts

However, when working with == and !=, you’ll run into issues when working with different types. In these cases, they’ll try to coerce the values, unsuccessfully.

For those unfamiliar, the “eval” function gives us access to JavaScript’s compiler. Essentially, we can execute a string’s result by passing it as a parameter of “eval”.

Not only will this decrease your script’s performance substantially, but it also poses a huge security risk because it grants far too much power to the passed in text. Avoid it!

Technically, you can get away with omitting most curly braces and semi-colons. Most browsers will correctly interpret the following:

However, consider this:

One might think that the code above would be equivalent to:

Unfortunately, he’d be wrong. In reality, it means:

As you’ll notice, the indentation mimics the functionality of the curly brace. Needless to say, this is a terrible practice that should be avoided at all costs. The only time that curly braces should be omitted is with one-liners, and even this is a highly debated topic.

What if, at a later date, you need to add more commands to this if statement. In order to do so, you would need to rewrite this block of code. Bottom line – tread with caution when omitting.

JSLint is a debugger written by Douglas Crockford. Simply paste in your script, and it’ll quickly scan for any noticeable issues and errors in your code.

“JSLint takes a JavaScript source and scans it. If it finds a problem, it returns a message describing the problem and an approximate location within the source. The problem is not necessarily a syntax error, although it often is. JSLint looks at some style conventions as well as structural problems. It does not prove that your program is correct. It just provides another set of eyes to help spot problems.”
– JSLint Documentation

Before signing off on a script, run it through JSLint just to be sure that you haven’t made any mindless mistakes.

This tip has already been recommended in the previous article in this series. As it’s highly appropriate though, I’ll paste in the information.

Javascript tips and tricks
Javascript tips and tricks

Remember — the primary goal is to make the page load as quickly as possible for the user. When loading a script, the browser can’t continue on until the entire file has been loaded. Thus, the user will have to wait longer before noticing any progress.

If you have JS files whose only purpose is to add functionality — for example, after a button is clicked — go ahead and place those files at the bottom, just before the closing body tag. This is absolutely a best practice.

When executing lengthy “for” statements, don’t make the engine work any harder than it must. For example:

Notice how we must determine the length of the array for each iteration, and how we traverse the dom to find the “container” element each time — highly inefficient!

Bonus points to the person who leaves a comment showing us how we can further improve the code block above.

Don’t always reach for your handy-dandy “for” statement when you need to loop through an array or object. Be creative and find the quickest solution for the job at hand. This is one of the best practices to learn javascript tips and tricks.

I won’t bore you with benchmarks; you’ll just have to believe me (or test for yourself) – this is by far the fastest method!

Using native methods (like join()), regardless of what’s going on behind the abstraction layer, is usually much faster than any non-native alternative.
– James Padolsey,

“By reducing your global footprint to a single name, you significantly reduce the chance of bad interactions with other applications, widgets, or libraries.”
– Douglas Crockford

Notice how we’ve “reduced our footprint” to just the ridiculously named “DudeNameSpace” object.

It might seem unnecessary at first, but trust me, you WANT to comment your code as best as possible. What happens when you return to the project months later, only to find that you can’t easily remember what your line of thinking was. Or, what if one of your colleagues needs to revise your code? Always, always comment important sections of your code.

Always compensate for when JavaScript is disabled. It might be tempting to think, “The majority of my viewers have JavaScript enabled, so I won’t worry about it.” However, this would be a huge mistake.

Have you taken a moment to view your beautiful slider with JavaScript turned off? (Download the Web Developer Toolbar for an easy way to do so.) It might break your site completely. As a rule of thumb, design your site assuming that JavaScript will be disabled. Then, once you’ve done so, begin to progressively enhance your layout!

Consider the following code:

Not only is this code inefficient, but it also functions in the same way as the “eval” function would. Never pass a string to SetInterval and SetTimeOut. Instead, pass a function name.

At first glance, “With” statements seem like a smart idea. The basic concept is that they can be used to provide a shorthand for accessing deeply nested objects. For example…

— instead of —

Unfortunately, after some testing, it was found that they “behave very badly when setting new members.” Instead, you should use var.

There are multiple ways to create objects in JavaScript. Perhaps the more traditional method is to use the “new” constructor, like so:

However, this method receives the “bad practice” stamp without actually being so. Instead, I recommend that you use the much more robust object literal method.

Note that if you simply want to create an empty object, {} will do the trick.

“Objects literals enable us to write code that supports lots of features yet still make it a relatively straightforward for the implement of our code. No need to invoke constructors directly or maintain the correct order of arguments passed to functions, etc.” –

“javascript tips and tricks”

The same applies for creating a new array.

“A common error in JavaScript programs is to use an object when an array is required or an array when an object is required. The rule is simple: when the property names are small sequential integers, you should use an array. Otherwise, use an object.” – Douglas Crockford

…Should be rather self-explanatory. I doubt there’s any real speed improvements here, but it cleans up your code a bit.

So there you have it. 15 essential javascript tips and tricks  for fresher Javascripters. Let me know your quick tips! Thanks for reading. What subject should the third part in this series cover?

Follow me on Twitter, or subscribe to the Javascript Solutions Feeds for more daily web development tuts and articles.

And be sure to check out the thousands of JavaScript items available on Envato Market. There’s sure to be something there to help you with your JavaScript development.


Leave a Comment

Your email address will not be published. Required fields are marked *