In JavaScript, there are two ways to add comments:
-
Single-Line Comments:
For single-line comments, you use
//
. Anything after//
on a line is considered a comment, and it won't be executed by the JavaScript engine.// This is a single-line comment let variable = 42; // This is also a single-line comment
-
Multi-Line Comments:
For comments that span multiple lines, you enclose the comment within
/* */
./* This is a multi-line comment */
These comments are ignored by the JavaScript interpreter and are useful for adding explanations, notes, or annotations within your code. They are beneficial for other developers (or even yourself) to understand the code's logic and functionality.