Why would you want to prevent code lines from running by using comments?

Could anyone clarify when multi-line commenting recommended? I’m not certain what the lesson means by “Multi-line comments are often best suited to prevent a block of code from running.” Or maybe I’m not sure the reason to prevent code from running . Thank you

// single line comment
 * multi-line
 * comments
 * permit
 * longer
 * messages

function foo(bar) {
    a function to filter 'foo' out of bar
    bar is an array
  return bar.filter(x => x != 'foo')

console.log(foo(['foo', 'bar', 'foo', 'bar']));
// ['bar', 'bar']

Say you wrote the following:

if (this === that) {
  console.log(this + ' is the same as ' + that);
} else {
  console.log(this + ' is not the same as ' + that);

Then you wonder if another method might work as well, but you don’t want to delete what you have because it took a lot of hard work to come up with :wink:, so you do this:

/*if (this === that) {
  console.log(this + ' is the same as ' + that);
} else {
  console.log(this + ' is not the same as ' + that);

console.log(this === that ? `${this} is the same as ${that}` : `${this} is not the same as ${that}`;

If it works, you can delete the code you commented out. If it doesn’t, you can delete the new code, and un-comment the commented out code. Now you are back to where you started, and didn’t have to re-type your original code. It is also useful for tracking down errors. You may want to comment out a block of code to see if the rest of the code is working correctly. Hope this helps!


Just a quick one - is it possible to use /* */ to make a single line of code inactive also? This would seem like the better method, especially as the code develops and comments may grow.

1 Like


 /* console.log(2); */

When I am commenting out parts of the code I only use single-line comments, even when I have to comment out a very large block of code. Reason for this is that my code editor allows me to quickly comment/uncomment selected lines with the ctrl + / shortcut. And there are many situations when in the process of coding / debugging I want to uncomment a single line that is in the middle of the commented block. In these cases // comments are much easier to handle.


Love the explanation.


In the words of Ian Malcolm: Well, There It Is.
Perfect explaination!


Great explanation. So clear.

1 Like

u can use it to know where exactlythe problem is in ur code so u try to take off everything else and run ur code. if the output is wrong that means there is problem righ there. it s beter than having all the code running at the same time. this is where i usually use it , idk about the others xD .
Or u can just describe the details of a part of a code that u wrote so the others understand.

Whew, this helped a lot. Thanks!

1 Like

Thanks for this explanation! :grinning:

1 Like

Dude, thank you so much. This makes way more sense than the lesson. They should revise and add your explanation to the lesson from now on

1 Like

One way of using multi-line comment can be at the time when you think to erase some part of your code. For instance, if there are two conditions in your code and if you are thinking to delete one of them, instead of deleting you can put those lines in comment. By doing this, that part of the code will be available whenever you want it.

Thank you! I knew the meaning of the practice now I know explained!

1 Like

Yes but why would we need to comment out the lines in the first place? I’m not understanding that. Does it just mean we don’t want them to run? That seems like the logical explanation but I am still unsure, so hopefully you can remove my confusion. Thank you.

Precisely. The commented code will not be parsed, but we still have it on hand to fall back to if need be.

Great, Thanks a bunch.

1 Like

Still a tad confused. Do both methods do the same thing? So you are not just putting in a comment (note) so you can figure out your code later, you are actually preventing it from running. /* */ prevented the code from running but // didn’t in my lesson.

A comment in any form is ignored.

[a, b, c] = [1, 2, 3]
//a = b + c
c = a + b

That last statement will execute, but the one before it will be ignored.

Is there a way to put in notes so you can easily see what your code is doing if you come back later without using comment which will ignore the code?