Source code guides

Make a compact JavaScript function readable

·

Formatting can make a nested branch easier to follow. The identifiers in the original source give you a way to compare the compact and formatted versions.

JavaScript Beautifier

Inspect a nested return statement

Format function price(n){if(n>0){return n*2}return 0} with two spaces. The if statement moves inside the function body and the return inside the branch is indented again. Choose four spaces and repeat to see the same nesting more clearly.

Prettier formatting options

Interpret the width setting

Choose a width of 40 for a long call with several arguments. The formatter can place the arguments on separate lines. A single long string literal can still extend past that column because splitting it changes the source expression.

Prettier formatting options

Keep a small diff reviewable

If you need to review a one-line third-party callback, format a copy and locate the return statements and block boundaries first. A width setting can wrap long expressions but cannot invent meaningful variable names or recover explanatory comments removed before distribution.

For a code review, keep formatting changes separate from a logic edit. Compare behavior in the project that owns the source before replacing its original file.

Common questions

Will formatting restore descriptive names?

The parser keeps the identifiers it reads. A variable named n stays n in the formatted file.

What if the function ends halfway through a statement?

The formatter reports a parse error. Close the missing expression or delimiter in the source and format it again.

Sources

Put it into practice.

Related guides