Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder Krupski's Avatar
    Join Date
    Dec 2010
    Location
    United States of America
    Posts
    505
    Thanks
    39
    Thanked 47 Times in 46 Posts

    Declare variables question

    THIS IS NOT HOMEWORK

    Hi all,

    Simple question: Is it "legal" to conditionally declare variables?

    For example:

    First
    Code:
        if (name) {
            var age = 54;
            var year = 1957;
        }

    Second

    Code:
        var age;
        var year;
        if (name) {
            age = 54;
            year = 1957;
        }
    Is "First" wrong and "Second" right? Or are they both equally OK?

    Thanks.

    -- Roger

    Edit:
    Please explain your answer.
    "Anything that is complex is not useful and anything that is useful is simple. This has been my whole life's motto." -- Mikhail T. Kalashnikov

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,378
    Thanks
    11
    Thanked 592 Times in 572 Posts
    the second is arguably easier to maintain among a group of coders, but both work fine as far as javascript is concerned.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #3
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Javascript isn't 'block scope' meaning any variable declared inside blocks (such as if) can be accessed outside, and vice versa.

    So in short, no, in javascript, it makes no difference whatsoever.

    Generally though, a lot of people consider it best practice to declare them outside the block (like in second example), purely becuase this help reduce confusing for coders moving from block scoped languages, like C, C#, C++ etc.
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    though there is an issue with the first one. if the condition is not executed then on the next call to the variable it is instantiated as global variable, which might interfere with other parts of your script. the second version makes sure the variables are local regardless of the condition.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    lopix (12-15-2011)

  • #5
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Quote Originally Posted by Dormilich View Post
    though there is an issue with the first one. if the condition is not executed then on the next call to the variable it is instantiated as global variable, which might interfere with other parts of your script. the second version makes sure the variables are local regardless of the condition.
    Very true.
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •