DEV Community

Ayobami Ogundiran
Ayobami Ogundiran

Posted on • Edited on

Commenting in JavaScript

Welcome to this lesson, in this lesson, we will talk about commenting in JavaScript.

What are Comments?

Comments refer to lines or blocks of inexecutable expressions or explanation about some certain piece of code.

There are so many reasons to comment your code but below is the basic reason:

It explains the part of the code that may be difficult for fellow developers to understand or for the owner of the codebase to remember its uses after leaving the codebase for a while.

How to use comments in JavaScript?

Single line commenting

Single line comments start with double forward slashes. Anything written after the double forward slashes will not be executed by JavaScript. E.g

//call function tick every second.
setInterval(tick, 1000);
Enter fullscreen mode Exit fullscreen mode

Or you can put the comment by the side of the code like below:

let name; // declares name
name = "Ayobami"; //the value of name is set to Ayobami
Enter fullscreen mode Exit fullscreen mode

This takes us to multi-line comments.

Multi-line commenting or Block-commenting

What are multi-line comments?

Multi-line comments are written between a star-forward slash and a star-forward slash as in below:

/* Get the element with its id and 
set its content to 5 */
const element = document.getElementById("id");
element.innerHTML = 5;
Enter fullscreen mode Exit fullscreen mode

Viola! We are done with commenting.
See you in the next lesson!

One more thing

Are you having difficulties to learn and understand JavaScript and build projects with it? JavaScript for a Total Novice teaches JavaScript and Project Making Fundamentals with simple illustrations and examples that make everything so easy. You can now handle any difficult projects without fear.

Don't trust me, get a free previous to judge by yourself: https://bit.ly/3o3TMyg

Top comments (2)

Collapse
 
rahxuls profile image
Rahul

Basic and awesome

Collapse
 
codingnninja profile image
Ayobami Ogundiran

Yeah! Thanks.