Please enable JavaScript to use CodeHS

CodeHS Glossary


Indentation JavaScript

Indentation is the visual structure of how your code is laid out. It uses tabs to organize code into a hierarchy. Having correct indentation is a crucial aspect of programming style. ---------- **How to indent your code** Use the tab key to insert a tab before a line of code or use the delete/backspace key to remove a tab. Do **not** use spaces to indent your code. Using tabs allows you to have a consistent length, and having spaces makes it easy to have code that is misaligned. Mass indentation: Indenting multiple lines at once. Select the code you want to indent and hit TAB //forward SHIFT+TAB //back ---------- **Style of indentation** There are two acceptable ways to use curly braces. Either way is fine for students to use as long as they are consistent throughout the program. 1: Same line { The } should line up vertically with the first letter of the word that starts the block that it’s closing. i.e. the f in function or the `i` in `if`. Every `}` should be on its own line of code with the exception of an `if/else`. function something() { if (condition()) { command(); } } function something() { if (condition()) { command(); } else { otherCommand(); } } 2: New line `{` The `}` should line up vertically with the `{` that it belongs to. Every `{` and `}` should be on its own line of code. function something() { if (condition()) { command(); } } ---------- **Why is Indentation important?** 1. Code that isn’t indented isn’t as easy to read. 2. Indentation is especially important when using multiple loops, functions, and if statements. The indentation gives you a good visual way to see what commands are inside vs. outside of a loop or if statement. 3. All programmers indent their code. If you have poorly indented code, it’s a clear giveaway that you’re an amateur programmer. 4. Some languages (Python, for example) require code to be indented correctly for it to work. The functionality actually depends on the indentation. This is not the case for Javascript, but it is better to practice good habits. ---------- This [video][1] shows you some examples on how to indent your code for some complex Karel programs. [1]: http://codehs.com/videoplayer/6