Proper indentation in javascript

15,905

Solution 1

Here you go: And like TGH says, there is no "right way", only the way you and your collegues agree on. As long as its consistent and you all agree, have at it.

this will help with prettying up your code: http://jsbeautifier.org/

the following will help you with some standards.

https://github.com/v0lkan/o2.js/blob/master/CONVENTIONS.md

https://github.com/rwaldron/idiomatic.js/

http://javascript.crockford.com/code.html

http://contribute.jquery.org/style-guide/js/

http://addyosmani.com/blog/javascript-style-guides-and-beautifiers/

Solution 2

Coding style is generally decided on per-project. For example:

The one caveat is with Automatic Semicolon Insertion, primarily regarding a few "restricted productions" which do not allow line-breaks.

return            // returns `undefined`
{                 // becomes a block rather than an `Object`
    key: "value"  // and a label rather than a property
};

Though, this brace style is safe in most other cases:

function foo(bar)
{
    console.log(foo, bar);
}
Share:
15,905

Related videos on Youtube

Admin
Author by

Admin

Updated on September 14, 2022

Comments

  • Admin
    Admin about 1 year

    I'm fairly new to coding what are some good rules of thumb to make my code easy to read to me and others. When do I indent where should my curly braces be? If you have any resources for websites that are good tutorials for this please link me. I've finished codeacademy and a lot of other starter resources for js. I still seem to be struggling to figure out how to make my code look pretty and readable. Thanks for everybody's help and suggestions in advance!

  • Dan Hlavenka
    Dan Hlavenka about 10 years
    +1 for jsbeautifier.org - a great resource for prettying up your code.