5 Basic Tips For Bug Free JavaScript Syntax

Magazine 21 Mar , 2013  

Buffer

After Running JavaScript now question arises that how you will be safe from bad syntax ?  I’ve 5 Basic things for you to be away from those bugs …

0005737_detail1. JavaScript Identifiers :

JavaScript Identifiers get used by programmers to to give variables and functions a unique name so later can subsequently be referred to by that name. The name of an identifier must follow a few rules:

(a). First Characters should not be number

(b). Should not be Reserved keyword

(c). Should only be composed of letters, numbers, dollar signs, and underscores.

//Following are some good Example of Javascript Identifiers
var myFirstName = "Ankita";
var mySecondName = "Gupta";
var my_current_city = "Noida";
var $my_website_name = "codesupport.info";
var _my_profession_$ = "developer";

2. Variable definition : Variables can be defined using multiple var statements, or in a single combined var statement.

//Example of single vs multiple var declarations
var first_var = 50;
var second_var = function() { ... };
var thirdv = second_var( test );

// And this also works 
var test4 = 1,
    test5 = function() { ... },
    test6 = second_var( first_var );

3.Reserved Words :

JavaScript has a number of “reserved words,” or words that have special meaning in the language. You should always avoid using these words in your code except when you really want to use them. You can Find it Here !

4.whitespaces :

Whitespace is also ignored in JavaScript. There are many tools that will strip out all the whitespace in a program, reducing the overall file size and improving network latency.

// Semantic whitespace promotes readability.
// Readable code is good!
var foo = function() {
  for ( var i = 0; i < 10; i++ ) {
    alert( i );
  }
};
foo();
// This is much harder to read!
var foo=function() {for(var i=0;i<10;i++){alert(i);}};foo();

5. Comments :

Commenting is always a good idea to implement, By commenting system code will be very easy to debug not only for you but for others programmers too, we have facility of both single line and multiple line comments too as follows

// Single and multi line comments.


// This is an example of a single line comment.
/*
 * this is an exampleof a
 * multi line
 * comment.
 */

So remember this in your mind during programming with JavaScript, Happy Coding 🙂

, , , , , , , ,