javascriptvariable-declaration

Can a `var` declaration be split into multiple lines without repeating the `var` keyword?


I have many variables in my javascript function and don't fit on one line declaration.

var query_string = $('list_form').serialize(), container = $('list_container'),position = container.cumulativeOffset(), layer = $('loaderLayer'),
            remote_url = "/web/view/list_offers.php?" + query_string;

Is there a way to define these variables on multiple lines with only one var keyword?


Solution

  • var query_string = $('list_form').serialize(),
        container = $('list_container'),
        position = container.cumulativeOffset(),
        layer = $('loaderLayer'),
        remote_url = "/web/view/list_offers.php?" + query_string;
    

    If I recall it correctly, that is the pattern advocated by Doughlas Crackford to declare multiple variables that script needs.

    Quick Tip: Beawere of JavaScript Hoisting :)