JavaScript statements, semicolons, and line breaks
Use semicolons to mark the end of JavaScript declarations and expression statements such as function calls. Automatic semicolon insertion (ASI) applies in specific situations. A line break can continue the previous expression or end a return earlier than intended.
Separate statements with semicolons
Separate two declarations on one line with a semicolon. A semicolon at the end of the first declaration also works when it is on its own line. An if statement with a braced body does not require a semicolon after that block.
const first = 2;
const second = 3;
console.log(first + second); // 5
const left = 'A'; const right = 'B';
console.log(left + right); // 'AB'
When a new line continues an expression
The expression after + is incomplete, so the second line supplies the missing operand. Even a complete-looking first line can absorb what follows if the grammar allows it.
const total = 2 +
3;
console.log(total); // 5
Run both blocks in a browser console. Without a semicolon, [0] indexes the string on the previous line and label receives A. End the declaration with a semicolon to keep AB. The standalone array expression then runs separately and its result is discarded. A line starting with ( can pose a similar problem. See the automatic semicolon insertion rules.
{
const label = 'AB'
[0].toString();
console.log(label); // A
}
{
const label = 'AB';
[0].toString();
console.log(label); // AB
}
Keep the return value on the same line
The first function ends at return and returns undefined. The braces that follow are treated as a block. Start the expression on the same line as return or put the opening parenthesis on that line to wrap a longer expression. See the return statement rules.
function wrong() {
return
{ total: 5 };
}
console.log(wrong()); // undefined
function correct() {
return (
{ total: 5 }
);
}
console.log(correct().total); // 5
Compare both return forms in a complete example
Save the complete example as statement-example.html in UTF-8 and open it in a browser. Select the comparison button. The same-line version displays 5 and the version with a line break after return displays undefined. Use the reset button to restore the prompt.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare semicolons and line breaks</title>
</head>
<body>
<h1>Compare line breaks after return</h1>
<button id="compare" type="button">Compare results</button>
<button id="reset" type="button">Reset</button>
<pre id="output" aria-live="polite">Select Compare results to begin.</pre>
<script>
function resultOnSameLine() {
return { total: 5 };
}
function resultAfterNewline() {
return
{ total: 5 };
}
const output = document.querySelector('#output');
document.querySelector('#compare').addEventListener('click', () => {
const safe = resultOnSameLine();
const broken = resultAfterNewline();
output.textContent = `Same line: ${safe.total}\nLine break after return: ${String(broken)}`;
});
document.querySelector('#reset').addEventListener('click', () => {
output.textContent = 'Select Compare results to begin.';
});
</script>
</body>
</html>




