VOOZH about

URL: https://www.digitalocean.com/community/tutorials/how-to-write-comments-in-javascript

⇱ How To Write Comments in JavaScript | DigitalOcean


How To Write Comments in JavaScript

Updated on August 30, 2021
πŸ‘ How To Write Comments in JavaScript

Introduction

In programming, our first consideration is usually the machine β€” how the computer is reading and interpreting the code we write. However, it’s equally important to consider the people who will be reading and working with the code. Whether you’re working with a team or on your own, you will need to learn to properly comment and structure your code for human readers.

Comments are annotations in the source code of a program that are ignored by the interpreter, and therefore have no effect on the actual output of the code. Comments can be immensely helpful in explaining the intent of what your code is or should be doing.

As a developer, it can be frustrating to delve into code written by someone else that was not properly commented, and it’s remarkably easy to forget what your own code meant when you’re no longer immersed in the context of a program. Commenting your code early on will reinforce good programming habits throughout your career to avoid these issues later on.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

Tutorial Series: How To Code in JavaScript

JavaScript is a high-level, object-based, dynamic scripting language popular as a tool for making webpages interactive.

About the author(s)

Software engineer and open source creator

Community and Developer Education expert. Former Senior Manager, Community at DigitalOcean. Focused on topics including Ubuntu 22.04, Ubuntu 20.04, Python, Django, and more.

Still looking for an answer?

Was this helpful?

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Hey, we forgot to mention /*! multiline comment blocks!

This blog is very clear about the content, and this can be useful to read and learn more knowledge from this blog.

You might at least mention that the /** */ syntax is for jsdoc and point people to the jsdoc docs.

Thank you for the article! Basing on our experience in GBKSOFT, it’s pretty useful when the team is working on the project. I assume that comment may come in handy when you have a cross-functional team and work on Node.js so everyone who knows JS can check the code.

It should be noted that comments should be only put where they are needed, as they need to be maintained along the code, and become easily out-of-date, and become more confusing than helpful.

Code readable by itself should be preferred over commented code. In many of the examples above the comments are not necessary because the code is self explaining, e.g.:

// Initialize a function
function alphabetizeOceans() {
 // Define oceans variable as a list of strings
 const oceans = ["Pacific", "Atlantic", "Indian", "Antarctic", "Arctic"];

 // Print alphabetized array to the console
 console.log(oceans.sort());
}

Here’s an article about that topic, and you’ll find many more on the internet: https://www.freecodecamp.org/news/code-comments-the-good-the-bad-and-the-ugly-be9cc65fbf83/

πŸ‘ Creative Commons
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
  • Deploy on DigitalOcean

    Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and AI-native businesses

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

The developer cloud

Scale up as you grow β€” whether you're running one virtual machine or ten thousand.

Start building today

From GPU-powered inference and Kubernetes to managed databases and storage, get everything you need to build, scale, and deploy intelligent applications.

Β© 2026 DigitalOcean, LLC.Sitemap.