Ad space (banner)
🟨JavaScript Lessons
Lesson 18 / 69

How to Write Comments

In this lesson you'll learn how to write comments in JavaScript so you can write code that's easy to understand even when you come back to it later. This is for beginners searching "JavaScript how to comment out."

A comment is a "note for humans" that has no effect on how the program runs. You write comments so that you (or someone else) can understand the code more easily later. // starts a single-line comment, and /* */ wraps a multi-line comment. Code written inside a comment doesn't get executed, so comments are also often used to temporarily disable code (commenting it out).

The sample code adds an explanation of a variable with a single-line comment, and writes a longer explanation with a multi-line comment. Adding a short note to the right of, or above, a variable — like // price of the item (yen) — is a style used very often in real projects. Comments can be written in either English or your team's preferred language, but the basic rule is to follow your team's conventions.

A common beginner stumbling block is writing so many comments that the code actually becomes harder to read. "What is happening" can often be understood just by reading the code itself, so it's more effective to use comments for "why this processing is needed" instead. Conversely, complex processing should get comments proactively — it's about quality over quantity.

In real development, whether or not comments exist makes a huge difference when working as a team. When you or a teammate looks back at the code months later, a comment that conveys the original intent can drastically speed up fixes and bug investigations. How well you write comments is treated as part of your real-world coding skill.

Many code editors have a keyboard shortcut for commenting out a selected range all at once, which is very handy when you want to temporarily disable code to check how something behaves. It's well worth using actively.

A special comment format called JSDoc (written as /** ... */, describing a function's explanation, arguments, and return value) causes many editors to automatically pop up that function's description, improving efficiency for team development.

JavaScript
OUTPUT

💡 Anything passed to console.log() appears in the output below.

Ad space (banner)
Ad space (in-article)