In programming, we use comments to comment code literally or to deactivate code without deleting it. There are two different main types of comments, the single line
and multi line
comments. In this course, I will use the single line
comment in most of my demonstrations.
Single Line Comment
// This is a single line comment
double slash
is used for commenting on a single line of code.Usually, we use this to describe what the code is doing. Also, remember javascript will completely ignore anything that is a comment. So anything that comes after //
will not be executed and this can be useful in dividing our code into different sections and explaining what other pieces of code are doing in our code.
// sum of two numbers
let x = 10;
let y = 20;
console.log(x,y) // 10 20
let total;
total = x + y;
console.log('Sum of x and y is:', total); // Sum of x and y is 30
single-line comments
.
Multi-Line Comment
Now, let's look into multi-line comment
. It is generally used to comment out multiple lines of code that we don't want to be executed for some reason.
/*
This
is a
Multi Line
Comment
*/
/*
and end with */

In VSCode or in the majority of the code editors, you use the Command + /
or Ctrl + /
shortcut to comment out code. According to the necessity, the editor will care what type of commenting method to use.