How to Format JavaScript
Published 2026-08-10
3 min read
By CoShareX
JavaScript (and TypeScript) is the primary scripting language of the modern web. Keeping your codebase readable and formatted reduces cognitive load and helps prevent bugs.
In this guide, we'll cover key JavaScript formatting standards.
Basic Formatting Rules
To keep your code clean:
- Indent block statements (usually with 2 spaces).
- Place a space after control flow keywords (e.g.
if (condition)). - Keep braces (
{}) consistent (most teams use the K&R style, opening braces on the same line).
Unformatted JavaScript:
javascript
1
function greet(user){if(user.active){console.log("Hello "+user.name);}}Formatted JavaScript:
javascript
1
2
3
4
5
function greet(user) {
if (user.active) {
console.log("Hello " + user.name);
}
}Featured Tool
JavaScript Formatter
Format, beautify and pretty-print JavaScript/TypeScript code.
Semicolon Consistency
Choose either to use semicolons explicitly at the end of statements, or omit them entirely. Consistency across your codebase is key to keeping it clean and easy to read.