JavaScript - Comments

Overview

Estimated time: 5–10 minutes

Use comments to explain why code exists. Keep them up-to-date.

Learning Objectives

  • Use single and multi-line comments.
  • Understand JSDoc basics for tooling.

Single-line

// This is a single-line comment
const x = 1; // trailing comment

Multi-line

/*
  Multi-line comments are useful
  for file headers or longer notes.
*/

Documentation Comments (JSDoc)

/**
 * Adds two numbers.
 * @param {number} a
 * @param {number} b
 * @returns {number}
 */
function add(a, b) { return a + b; }