Home>

I have a question regarding the subject.

Unlike other languages, Javascript winds up variables, so
From ES6, it is better to declare variables with let,
(Although there is a let)

If i declare it with let,
Is it good for management to define it near the method to be used?

Or just like var,
Is it easy to maintain by declaring all variables at the beginning of the function?
Or is it a non-confusing way of writing?

For the time being, I checked the coding rules regarding the variable declaration position of js,
I can't find anything in particular, and define the variable at the beginning or just before the method to be used.
I would appreciate it if you could teach each coding rule.

I look forward to working with you.

  • Answer # 1

    There has been a long-standing debate about whether to declare local variables at the beginning of a block or where they are needed.

    In older C languages ​​prior to C99 (the C specification released in 1999), local variable declarations had to be grouped together at the very beginning of the block. More than 20 years after the release of C99, all major C compilers support C99 and above, but some people still teach that "declaring local variables is the very beginning of a block".For some reason, even in languages ​​other than C (Java, C #, etc.)!Conversely, people who have learned from Java, C #, etc. without knowing the old C language tend to start where they need local variables. This is especially true for those who started with Python or Ruby, where the first assignment doubles as a declaration (that is, there is no separate declaration of a local variable).

    I will omit each claim, advantage/disadvantage, etc. because I do not have time now, but although it was not found in JavaScript, there seems to be a coding style guide that specifies the position of local variables in other languages, so I will omit them. Please try to reference.

    Finally, to conclude, the most convincing claim for me is, "If each function (method) is short enough, the position of the local variable will not change much in either case." ..


    I'm a CoffeeScript sect, so I'm not eligible to participate in this discussion.

  • Answer # 2

    If the design allows you to determine the variables used within the scope, it is better to make the declaration ahead as much as possible.
    It will be easy to manage
    However, if you're used to the looseness of js, you can't get rid of the habit of declaring it when you use it.

  • Answer # 3

    I would appreciate it if you could teach each coding rule.

    If you do it as a team, it will change for each team, so
    In many cases, everyone decides the rules.

    For example, in my team

    When defining with jQuery, in order to speed up the processing as much as possible,
    DOM acquisition variable in jQuery is defined at the beginning,
    When calling, call from the definition variable as much as possible and use


    There is a rule called.

    so,
    honestly speaking,
    Exists by site or projectIt is no exaggeration to say that.
    So, rather than looking badly now and getting a weird habit
    Isn't it better to write and understand the nature of the language now?
    I think.

    Whether to use ASI depends on the level of religious war

    However, JavaScript has a bit of awkward specs, so
    Thereby,Some kind of commitmentStrong insistence
    Some sites are strictly regular.

    ASI (Automatic Semicolon Insertion)
    In a syntax that the computer cannot interpret without a semicolon,
    It is a specification that automatically complements the required semicolon.

    JavaScript is originally a language that requires a semicolon to be written,
    Because it is possible to read a large number of syntax files in a mixed manner
    For example, even if you write it firmly in your own code
    In the library I'm loading, I forgot to write it, and sometimes an error occurs,
    The process will stop there.
    Complementing such forgetfulness, the compiler automatically inserts a semicolon and interprets it.
    The specification is ASI.

    Due to the existence of this
    Some groups shouldn't have a semicolon and some should.Split in twois.

    In particular, those who cannot enter, in order to avoid the inconvenience that occurs because they cannot enter
    I personally feel that there is a strong tendency to set strict description rules.

    JavaScript ASIOr
    JavaScript semicolonIf you look up, you will find articles with various principles, so if you are interested, you should look it up.

    By the way, ISemicolon is definitely includedis.
    AnywayIf it is necessary as a specification, you should grasp and understand it yourselfBecause I think it is.
    However, as mentioned earlier, there are no correct or incorrect answers in either of them, as they are not in the realm of religious war.

    The digression has become longer, but that's it.

  • Answer # 4

    Consider the following case.

    for (let i = 0;i<10;i ++) {
        console.log (i);
    }
    console.log (i);

    In this case, the lifetime of i is limited to the for statement, so the lastconsole.log (i);Will fail.
    varLifespan is "in function" so it won't fail,let letThis is because the life of is "inside the block".

    in short,varDoesn't change much whether you define it at the beginning of a function or when you need it,constlet letIn the case of, it will change.

    This makes it clear that i is a loop variable, so I'm the one who declares it each time, not at the beginning.

    Declaring a variable at the beginning of a function is a remnant of the old C language writing style, which had the disadvantage of being less readable, resulting in a new writing style. Also,let letLifespanvarUnlike, it has been changed from a function to a block. Given that, I think it should be respected if the team has a rule, or if the existing code has that rule, but if not, you have to bother to declare it at the beginning of the function. I think it's a good idea to expand the scope.