2. Syntax Error
1. Missing Semicolon (;)
println(“n Welcome 1”) ; // Missing
for(var i= 0 ;i<10;i++)
2. Missing curly braces ({}).
for(var i= 0 ;i<10;i++){ } // Missing
function test (){ } // Missing
3 . Missing Open close braces ().
function test ( param1,param2 ) // Missing
4. Missing Commas (,).
var a = “A’” ,b = “B” // Oops, missing ',' here!
c = “C”, d = “D”;
3. JavaScript Is
case sensitive
• JavaScript is very case sensitive.
• Example :
function test1(inputContext){
println( “n inputContext ”+InputContext);
}
inputContext is different from InputContext.
4. Confusion between Concatenation and
Addition
1. Println (10 +2)
Type of 2 is Number – Addition
-- result : 12
2. Println (10 + “2”)
Type of 2 is String – Concatenation
--result : 102
5. Confusing single and
double quotes
1. Println ('Two’);
-- result is Two
2. Println ("Two's");
-- result is Two’s
3. Println ('Two's');
-- Error
6. Variable
without - var
• JavaScript is very permissive.
• It will silently declare it for you globally.
• Memory Leakage.
• Significantly reduce the chance of bad
interactions with other applications libraries.
7. Differentiate float numbers
from integer numbers
• Example:
var myNumber = 3.5;
var myResult = 3.5 + 1.0; //We use .0 to keep the result as float
1. No difference between float and integers.
2. Don’t use decimals to “convert” numbers to
floats.
var myNumber = 3.5;
var myResult = 3.5 + 1; //Result is 4.5, as expected
8. Don't Use Short Hand
• Curly braces should be omitted is with one-liners.
Example:
if(someVariableExists)
x = false
anotherFunctionCall();
It means:
if(someVariableExists) {
x = false;
}
anotherFunctionCall();
9. Missing HasOwnProperty
for Object
• Example :
for(key in object) {
variable = object[key] ; // includes object properties
}
}
• Solution:
for(key in object) {
if(object.hasOwnProperty(key) {
...then do something...
}
}
10. Eval is Evil.
• Give Access to JavaScript Compiler.
• Debug message arising from Eval are
unsearchable.
• Eval (String Parameter) then ,
– Decrease Performance
– Huge Security Risk .
11. Undefined Property
• Example:
var object ={
key1 = {},
key2 = undefined
}
- Running loop for checking existence of an elements
typeof object [key1] = object,
typeof object [key2] = undefined
typeof object [key3] = undefined
• Solution:
key2 = null
12. Misuse of Closures
• Example:
function(a, b, c) {
var d = 10;
function test (a,b,c,d){
return a+b+c+d;
}}
• Solution:
function(a, b, c) {
var d = 10;
function test (){
return a+b+c+d;
}}
13. Conditional Statements
• Example:
1. If(var1 = var2 ) // Assignment
-- if (var 1 == var 2)
1. for(var i=0;i<10;i) // execute in infinite loop
-- for (var i=0;i<10;i++)
1. for (var i=0;i< Array.length;i++)
-- var arrLen = Array.length;
for(var 1=0;i<arrlen; i++)
14. Overwriting/Overloading
function
• Overloading : Declare function more than Once
with different arguments.
• No function overloading in JavaScript.
• Last compiled function definition is used.
• This behavior is different from other
Programming Language.
Example :
-function test (param1)
- function test (param1, param2)
15. Missing Parameter
• Forgot to update to function calls when new
parameter add in function.
• Example:
– function(param1, param2){}
– Add new parameter Param3
– function (param1,param2,param3){
param3 = param3 II Default Value
}
16. JavaScript Code
Quality Tools
• Firebug
• Firebug is an extremely popular and well-
regarded front-end debugging tool.
• Drosera
Drosera is an excellent debugging tool for
Safari and WebKit-based browsers.
• NitobiBug
NitobiBug is a browser-based JavaScript object
logger and inspector