In JavaScript, there are two ways to comment code: single-line comments and multi-line comments.
Single-Line Comments:
Single-line comments begin with two forward slashes (//) and extend to the end of the line. They are used to add comments or notes about a particular line of code.
Example:
let greeting = "Hello, world!"; // This line sets the greeting variable to "Hello, world!"
Multi-Line Comments:
Multi-line comments begin with a slash and an asterisk (/*) and end with an asterisk and a slash (*/). They are used to comment out multiple lines of code or to add detailed explanations about a particular segment of code.
Example:
/*
This code section calculates the sum of two numbers
and stores the result in the variable 'sum'.
*/
let num1 = 5;
let num2 = 10;
let sum = num1 + num2; // The sum of num1 and num2 is stored in the 'sum' variable.