Naming Conventions and Commenting

Writing clean JavaScript code isn’t just about making things work — it’s about making your code easy for others (and your future self) to read and understand. Following good naming conventions and adding useful comments can make a big difference in how professional and readable your code feels.

camelCase vs snake_case

In JavaScript, the most common way to name variables and functions is using camelCase. This means you start with a lowercase word and then capitalize the first letter of each new word, like userName or totalAmount.

Example:

javascript
1
2
3
4
          let userAge = 25;
function getUserInfo() {
  // code here
}
        

snake_case, where you separate words with underscores like user_age, is more popular in languages like Python. In JavaScript, stick to camelCase unless you’re working with external data that uses underscores.

Meaningful names

Always choose names that clearly explain what the variable, function, or class is about. Avoid vague names like data, info, or stuff.

Bad example:

javascript
1
2
3
4
          let x = 10;
function doThing() {
  // what thing?
}
        

Better example:

javascript
1
2
3
4
          let userScore = 10;
function calculateFinalScore() {
  // clear what this does
}
        

The goal is to make your code easy to understand without needing extra explanations.

When to comment (and when not to)

Comments should explain why something is done, not what the code is doing if the code is already clear. Good comments add context, not noise.

Good comment:

javascript
1
2
3
4
          // Add bonus points if the user finished early
if (user.finishedEarly) {
  score += 10;
}
        

Bad comment:

javascript
1
2
          // add 10 to score
score += 10;
        

If your code is self-explanatory with good naming, you don't need to comment every little thing. Save comments for tricky logic, important notes, or future reminders.