Comments, Naming and Style

JavaScript ES2023 · ✓ verified by execution on 2026-08-11

Code is read far more often than it is written. Comments help humans understand why the code exists, while styling (like semicolons and line breaks) helps JavaScript understand how to run it.

Comments

Comments are used to add hints, notes, suggestions, or warnings to JavaScript code. This makes code easier to read, and the JavaScript engine completely ignores them.

Line comments use // and make all text following them on the same line into a comment:

javascript
function test() {
  // This is a line comment
  return 1;
}
console.log(test());
Output
1

Block comments use /* and */ to span multiple lines. Be careful though: multi-line comments cannot be nested!

javascript
/* Outer 
 /* Inner */ 
 let x = 1; */
console.log(x);
This example raises an error (on purpose)
SyntaxError

Semicolons and ASI

In many languages, semicolons ; are required to end every statement. JavaScript is more forgiving: it uses Automatic Semicolon Insertion (ASI). When the engine encounters a line break, it automatically inserts semicolons to “fix” invalid token sequences.

javascript
let a = 1
let b = 2
console.log(a + b)
Output
3
let a = 1 let b = 2 console.log(a+b) let a = 1; let b = 2; console.log(a+b); ASI
Automatic Semicolon Insertion: the same three lines written without semicolons, and what the engine actually parses after ASI adds them.

However, ASI is not magic. There are times when relying on it causes bizarre behavior.

The Return Line Break Trap

What do you think happens if you place a line break immediately after the return keyword?

Predict the output javascript

Read the code. What exactly will it print? Commit to an answer before you look.

function test() {
  let a = 1, b = 2;
  return
  a + b;
}
console.log(test());
Output
undefined
javascript · visualize
function test() {
  let a = 1, b = 2;
  return
  a + b;
}
console.log(test());
undefined

JavaScript sees return and a line break, and assumes the statement is finished. It inserts a semicolon, making it return; (which returns undefined). The expression a + b becomes unreachable code.

When ASI Fails You

ASI looks at the token stream and only inserts semicolons when parsing would otherwise fail. It does not insert a semicolon if the next line could technically be part of the current line!

javascript
let b = 1;
let c = 0;
b
++c;
console.log(b, c);
Output
1 1

Here, ++ is not treated as a postfix operator for b because of the line break. The engine inserted a semicolon before ++, breaking the expected logic.

Semicolons are also not inserted inside a for loop’s head:

javascript
let count = 0;
for (let i = 0; i < 1; i++) {
  count++;
}
console.log(count);
Output
1

Edge Cases

Starting a line with [, (, or ` without a preceding semicolon causes the engine to attempt to parse the previous line as a function call, array index, or template tag. Always use semicolons if the next line begins with one of these characters!

javascript
let a = 1
let c = 3
(a + 2).toString()
This example raises an error (on purpose)
TypeError

Without a semicolon after let c = 3, JavaScript tries to evaluate 3(a + 2), treating 3 as a function!

Whitespace and line breaks do matter in JavaScript because of ASI.

Check yourself

Can you nest multi-line comments?

Reveal answer

No. The first */ closes the entire comment. — Multi-line comments (/* ... */) cannot be nested. The first */ encountered closes the comment.

What happens if you put a line break right after a return keyword?

Reveal answer

ASI inserts a semicolon, making the function return undefined. — Breaking right after the return keyword triggers Automatic Semicolon Insertion (ASI), causing the function to return undefined.

Are semicolons completely optional and safe to ignore in JavaScript?

Reveal answer

No, omitting semicolons before lines starting with [, (, or ` can lead to unexpected TypeErrors. — While optional in most cases, omitting semicolons before lines starting with [, (, or ` can lead to bizarre TypeErrors due to ASI failure.

Challenges

🐞 Bug Hunt +20 XP

Fix the function so it returns the expected array instead of undefined.

This code runs. It just does the wrong thing. Read it, find the defect, fix it — the tests below decide when you are right.

javascript
  • Test 1 — expects "3\n"
Need a hint? (−25% XP)

Bring the [ up to the same line as the return keyword to prevent ASI.

Show solution (0 XP)
function getNumbers() {
  return [
    1,
    2,
    3
  ];
}
console.log(getNumbers().length);

🐞 Bug Hunt +20 XP

Fix the syntax error caused by improper use of block comments.

This code runs. It just does the wrong thing. Read it, find the defect, fix it — the tests below decide when you are right.

javascript
  • Test 1 — expects "5\n"
Need a hint? (−25% XP)

Remove the nested /* and */ inside the main block comment.

Show solution (0 XP)
/* This is a comment
   This is a nested comment 
  End of comment */
let a = 5;
console.log(a);