Sunday, April 22, 2012

JavaScript:Tips and Tricks -2

Global variables-
Java script provides 3 different ways by which you can define a global variable-
1) Var statement - var a = 1;
2) window statement - window.b = 1; -> window is global object which is nothing but a container of all global variables.
3) using nothing - c = 1;
All above will work and will define a,b and c global variables in your javascript.

Semicolon insertion -
JavaScript has a mechanism that tries to correct faulty programs by automatically inserting semicolons.It sometimes inserts semicolons in places where they are not welcome. For example -
Following code will alert a - undefined
function testFunction() {
    var a = getValue();
    alert("a - " + a);
}              
function getValue() {
    return
    {
    status: 1
    };
}
Whereas changing the position of "{" in return statement in the following way gives the correct output-
function testFunction() {
    var a = getValue();
    alert("a - " + a.status);
}              
function getValue() {
    return{
    status: 1
    };
}
a.status - 1

In the first function as Javascript inserts ";" just after return statement, which gives faulty result.


Scope -
JavaScript uses the block syntax, but does not provide block scope, which means a variable declared in a block is visible everywhere in the function containing the block. For example the following function will alert b -2
function testFunction() {
    var a = 1;
    { var b = 2; }
    alert("b - " + b);
}
in c# this b wont be visible out of the block. To avoid confusion it is better to declare all variables at the top of each function.

typeof -
typeof is used to identify the Type of a variable. In Javascript it is little in consistent for different kind o variable. For example -
alert("typeOf(90.8) - " + typeof(90.8)); // - number
alert("typeOf(null) - " + typeof (null)); // - object
alert("typeOf(NaN) - " + typeof (NaN)); // - number
var myArray = new Array();          
alert("typeOf(myArray) - " + typeof (myArray)); // - object
Type of 90.8 is a number and type of NaN is also a number. Moreover the Javascript gives type of NULL as object, which is of course misleading.

parseInt -
parseInt is a function that converts a string into an integer.In javascript it stops when it sees a nondigit, so parseInt("16") and parseInt("16 tons") produce the same result. It would be nice if the function somehow informed us about the extra text, but it doesn't. If the first character of the string is 0, then the string is evaluated in base 8 instead of base 10. In base 8, 8 and 9 are not digits, so parseInt("08") and parseInt("09") produce 0 as their result. This error causes problems in programs that parse dates and times. Fortunately, parseInt can take a radix parameter, so that parseInt("08", 10) produces 8.

"+" operator -
The + operator can add numbers and concatenate strings.  If either operand is an empty string(or a string), it produces the other operand converted to a string. If both operands are numbers, it produces the sum.
Example -
alert( 1 + 2); // alert 3
alert(parseInt(1 + "2"));// alert 12

No comments:

Post a Comment