Explicit zero-initialization of vars

Discussion in 'Javascript' started by Archos, Dec 28, 2011.

  1. Archos

    Archos Guest

    Is it recommended make zero-initialization of vars explicit?

    By example:

    `var i = 0` instead of `var i` // for an integer
    `var s = ""` instead of `var s` // for a string
    Archos, Dec 28, 2011
    #1
    1. Advertising

  2. On Dec 28, 2:53 am, Archos <> wrote:
    > Is it recommended make zero-initialization of vars explicit?
    >
    > By example:
    >
    > `var i = 0`  instead of `var i` // for an integer
    > `var s = ""` instead of `var s` // for a string


    No.
    Michael Haufe (TNO), Dec 28, 2011
    #2
    1. Advertising

  3. 2011-12-28 10:53, Archos wrote:

    > Is it recommended make zero-initialization of vars explicit?


    There is no implicit zero-initialization. If you declare a variable
    without initializing it, its initial value is the special value
    undefined, of the special type undefined.

    > By example:
    >
    > `var i = 0` instead of `var i` // for an integer
    > `var s = ""` instead of `var s` // for a string


    Without the initializers (= 0 and = ""), both i and s would come to life
    with the value undefined. It's not an integer value, it's no a string
    value, it's - of type undefined. You can see this if you check out this:

    var i;
    alert(i);
    alert(typeof i);

    It is generally a good idea to initialize a variable on declaration, if
    a natural and useful initial value can be assigned to it, such as 0 for
    a counter or "" for a variable into which some string will be composed
    by catenating strings.

    It is not a good idea to initialize a variable with a dummy value just
    to make it initialized. Every variable _is_ initialized by language
    rules in JavaScript, and the special value undefined is often more
    useful than a dummy neutral value like 0 or "". If your code runs wild
    and uses the value of a variable to which no proper value has been
    assigned, it's better to "see" the variable value as undefined, as it
    logically is.

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
    Jukka K. Korpela, Dec 28, 2011
    #3
  4. Michael Haufe (TNO) wrote:

    > On Dec 28, 2:53 am, Archos <> wrote:
    >> Is it recommended make zero-initialization of vars explicit?
    >>
    >> By example:
    >>
    >> `var i = 0` instead of `var i` // for an integer
    >> `var s = ""` instead of `var s` // for a string

    >
    > No.


    It depends.


    PointedEars
    --
    Danny Goodman's books are out of date and teach practices that are
    positively harmful for cross-browser scripting.
    -- Richard Cornford, cljs, <cife6q$253$1$> (2004)
    Thomas 'PointedEars' Lahn, Dec 28, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jon

    app vars and cache vars

    Jon, Dec 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    392
  2. J.T. Conklin
    Replies:
    1
    Views:
    437
    David Hilsee
    Aug 11, 2004
  3. Linuxguy123
    Replies:
    7
    Views:
    683
    Paddy O'Loughlin
    Feb 20, 2009
  4. caccolangrifata
    Replies:
    18
    Views:
    392
    Chris Torek
    Jul 22, 2011
  5. Mike Schinkel
    Replies:
    2
    Views:
    139
    Aaron Bertrand [MVP]
    Jan 30, 2004
Loading...

Share This Page