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']
6 Likes

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!

111 Likes

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.
Thanks,
K

1 Like

Sure:

console.log(1);
 /* console.log(2); */
console.log(3);

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.

15 Likes

Love the explanation.

2 Likes

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

2 Likes

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.