<Slushman />

How to Write Comments in JavaScript, TypeScript, and React

Published May 26, 2023

TLDR: watch the YouTube video instead

How to Write Comments in JavaScript, TypeScript, and React

Comments play a crucial role in code documentation and collaboration. They help programmers understand the purpose and functionality of different sections of code, making it easier to maintain and debug. In this blog post, we will explore how to write comments in JavaScript, TypeScript, and React files. We will cover single-line syntax, multi-line syntax, docblocks, valid places to put comments, and invalid places to put comments.

Comment Syntax

Single-line syntax

To write a comment on a single line in JavaScript, start the line with two forward slashes (//). Anything after the slashes will be ignored by the interpreter. For example: javascript

// This is a single-line comment in JavaScript.

Multi-line syntax

JavaScript also allows multi-line comments using the /* */ syntax. This type of comment can span across multiple lines.

This is a multi-line comment
in JavaScript.

Docblocks, also known as JSDoc comments, are a specific type of comment used to document functions, classes, and variables. They provide structured information about the code and can be processed by documentation generators.

 * Calculates the sum of two numbers.
 * @param {number} a - The first number.
 * @param {number} b - The second number.
 * @returns {number} The sum of the two numbers.
function sum(a, b) {
  return a + b;

You can learn more about JavaScript in the book “JavaScript and jQuery: Interactive Front-end Web Development” by Jon Duckett.

Valid Places to Put Comments

In React files, comments can be placed anywhere within React components to explain their purpose, describe props, or document complex logic.

Invalid Places to Put Comments

In React files, comments should not be placed inside JSX tags or expressions, as they will be treated as part of the render output.


Comments are an essential tool for code documentation and understanding. In JavaScript, TypeScript, and React, you can use single-line syntax, multi-line syntax, and docblocks to provide context and explanations for your code. By utilizing comments effectively and placing them in valid locations, you can improve code readability and maintainability, making collaboration and debugging easier for yourself and your team.

Share this post!

Check out these related posts:

Write Comments in HTML

Learn the different ways to write comments in HTML , where you can and cannot put them, and how it can help you write code.