Last Updated: February 25, 2016
·
1.901K
· iondrimba

Declaring variables inline (a better way)

So lately I've seen some people complaining about declaring variables inline the way JSLINT imposes. People argue that is not good at all for debugging purposes and its hard comment stuff out and etc..

One thing I've been doing to overcome this problem, is to declare variables with a default value, and after that, I set the values I really want, overcoming the "you cant debug problem".

function Demo() {
    var personId = 0,
        personName = '',
        personAge = 0;

    // here I set the values I need
    personId = $(whatever-selector-id).val(); //convert this to number
    personName = $(whatever-selector-name).text();
    personAge = $(whatever-selector-age).val(); //convert this to number
}

Now I know the types I should expect of these variables, giving me a clearer way to read the code.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside