javascript common mistake
TRANSCRIPT
KNOWLEDGE TRANSFER AND SHARING
(Java script Common Mistake’s)
Syntax Error1. 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”;
JavaScript Is case sensitive
• JavaScript is very case sensitive.• Example :
function test1(inputContext){println( “\n inputContext ”+InputContext);
}
inputContext is different from InputContext.
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
Confusing single and double quotes
1. Println ('Two’);-- result is Two
2. Println ("Two's");-- result is Two’s
3. Println ('Two's'); -- Error
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.
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
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();
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... }}
Eval is Evil.
• Give Access to JavaScript Compiler.• Debug message arising from Eval are
unsearchable.• Eval (String Parameter) then ,– Decrease Performance– Huge Security Risk .
Undefined Property
• Example:var object ={
key1 = {},key2 = undefined}
- Running loop for checking existence of an elementstypeof object [key1] = object,typeof object [key2] = undefinedtypeof object [key3] = undefined
• Solution:key2 = null
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;}}
Conditional Statements
• Example:1. If(var1 = var2 ) // Assignment
-- if (var 1 == var 2)2. for(var i=0;i<10;i) // execute in infinite loop
-- for (var i=0;i<10;i++)3. for (var i=0;i< Array.length;i++)
-- var arrLen = Array.length;for(var 1=0;i<arrlen; i++)
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)
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 }
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.
• NitobiBugNitobiBug is a browser-based JavaScript object logger and inspector