none
JavaScript Variables RRS feed

  • Question

  • I'm not looking for performance gains, just curious.

    Is there any "benefit" of declaring variables as a particular type?  What I mean is doing something like:

    var path = new String("start");
    instead of
    var path = "start";

    I was wondering because if I use this variable 100 times (conditions, concatinations, etc...), would this save any type checking? Example:

    var path = new String("start");
    var num = 33;

    if(path == num)

    In this case could the parser first check the "type" of "num" and immediatly know that they are not equal without performing any more checks? Hopefully you get the point, I guess I'm just looking for a little insight into how these sort of things are handled inside the JavaScript parser. 

    How about the '+' operator?  How does the parser handle the difference between:

    var n = new String("1");
    n += 1;

    and

    var n = 1;
    n += 1;

    Can you give me an explanation (or point me in the direction of) how comparisons, concatinations, etc... are handled inside the parser?  Is there any "type checking" going on here?

    Friday, September 8, 2006 6:21 PM

Answers

  • >> Is there any "benefit" of declaring variables as a particular type? What I mean is doing something like:
    >> var path1 = new String("start");
    >> instead of
    >> var path2 = "start";

     

    No, there is no benefit to this coding pattern. Note that in your example you have not declared a variable as a particular type – JScript does not have the concept of a type constrained variable.

    Also, these two are not equivalent; the former creates a String object while the latter creates a primitive string. String is one of the intrinsic JScript objects (the others being Array, Boolean, Date, Function, Math, Number, RegExp, Object, Error, and the global object).

     

    The line,

    var path1 = new String("start");

    causes a String object to be created, and its constructor is invoked with the parameter "start". In JScript all function calls are bound dynamically; in terms of implementation then, at runtime the scope chain is searched until one is found.

     

    Whereas the line,

    var path2 = "start";

    causes a primitive string to be created.

     

    You can call any of the methods of the String object on a primitive string; JScript automatically boxes the primitive string to a temporary String object, calls the method, then discards the temporary String object. You can also use the String.length property with a primitive string. For example:

    x = path2.length; // assigns x the value 5

     

    The following code fragment further shows the difference between path1 and path2.

    WScript.echo(typeof(path1));  // prints out object
    WScript.echo(typeof(path2));  // prints out string

     

    As a further example, consider the following:

    var s1 = "Math.sin(45)";
    var s2 = new String("Math.sin(45)");
    WScript.echo(eval(s1));  // prints 0.850903524534118
    WScript.echo(eval(s2));  // prints Math.sin(45)

    You should use primitive strings unless you specifically need to use a String object.

     

    >> I was wondering because if I use this variable 100 times (conditions, concatenations, etc...), would this save any type checking? Example:

    >> var path = new String("start");
    >> var num = 33;

    >> if(path == num)

    >> In this case could the parser first check the "type" of "num" and immediately know that they are not equal without performing any more checks?

     

    “No” on both counts. Using a construct like var path = new String("start"); 100 times instantiates 100 String objects with the consequent search over the scope chain for the constructor.

     

    JScript is a loosely typed language. Loosely typed means you do not have to declare the data types of variables explicitly; it also means that the type associated with a variable may change at runtime. Hence the parser has to be conservative, and defer all type work to the runtime interpreter. Furthermore, the equality operator will do coercions between types in many cases. In the present case, at runtime, the object (path) is converted to its primitive value and then an equality comparison is done as though both were primitives. Refer “The Abstract Equality Comparison Algorithm” §11.9.3, rule #21 from the ECMAScript Language Specification (http://www.ecma-international.org/publications/files/ecma-st/ECMA-262.pdf).

     

    >> How about the '+' operator?  How does the parser handle the difference between:

    >> var n = new String("1");
    >> n += 1;

    >> and

    >> var n = 1;
    >> n += 1;

     

    The parser handles them similarly; it lets the runtime decide what type conversions to apply. Note that in your example you are using the += operator.

     

    Consider the first case:
    var n = new String("1");
    n += 1;

    In JScript, the ‘+’ operator is used for addition as well as string concatenation (refer “The Addition operator (+)”, §11.6.1 from the ECMAScript Language Specification). In the present case, at runtime, string concatenation happens as follows:

     

    (1) both addends are converted to primitives; the primitive integer 1 and the String object n are both converted to primitives (rule #5, 6)

    (2) both arguments are then converted to strings (rule #12, 13)

    (3) the strings are then concatenated resulting in “11” (rule #14, 15)

     

    Consider the second case:
    var n = 1;
    n += 1;

    Here the ‘+’ operator is used for addition, resulting in 2.


    thank you for your interest.
    pratap

    Tuesday, September 19, 2006 6:43 AM