How to Comment Out JavaScript

Learn how to comment out JavaScript code, what to watch out for, and how to prevent it from slowing down your website or app.

Published Categorized as JavaScript

Sometimes, as you write JavaScript code for your website or application, you may want to disable a few lines of code without necessarily deleting them. To do this, you can comment those lines out. This tutorial will show you how to do it.

Suppose you have the following two functions in your JavaScript code:

function funcOne(param) {
    console.log(param)
}

function funcTwo(param) {
    console.log(param)
}

For one reason or another, you want to comment funcTwo out.

Multi-Line Comments

To do this, add a forward slash followed by an asterisk (/*) to the beginning of the code block that you want commented out, and an asterisk followed by a forward slash (*/) to the end. By doing so, you turn the code block into a multi-line comment — and tell the JavaScript interpreter to not execute any of the code within that comment.

Here’s what the commented-out function looks like:

function funcOne(param) {
    console.log(param)
}

/*
function funcTwo(param) {
    console.log(param)
}
*/

You can also add notes to your comments to make your source code easier for others to read:

function funcOne(param) {
    console.log(param)
}

/* Deprecated - 2/2023
function funcTwo(param) {
    console.log(param)
}
*/

This is especially important if you’re part of a team or you work in a large corporation, and your code is part of a program that’s built collaboratively with other people who won’t know why and when you commented out this chunk of code.

Single-Line Comments

In JavaScript, you can also create single-line comments by adding two forward slashes (//) to the start of the line. This is useful when you want to temporarily disable a single line of code, like the calling of a function.

For example, you can turn this:

funcTwo("Hello world!");

Into this, which would once again prevent the interpreter from executing the code:

// funcTwo("Hello world!");

Things to Know

  • Choose the right method. Generally, single-line comments are best for commenting single lines out, and multi-line comments for commenting multiple lines out. If you’re still not sure which one you should use, check your team’s (or your company’s) coding style guide.
  • Close your comments correctly. Make sure to close the comments in the right place, or everything else in your code will be treated as a comment as well, which will case your website’s or application’s logic to break.
  • Keep two versions of your JavaScript files. Comments make your JavaScript files longer, and longer JavaScript files load slower. To prevent comments from slowing down your website or application, keep two versions of your JavaScript files — a prettified (dev) version with comments, and a minified (prod) version without.
Image courtesy of cottonbro studio /Pexels

By Dim Nikov

Editor of Maker's Aid. Part programmer, part marketer. Making things on the web and helping others do the same since the 2000s. Yes, I had Friendster and Myspace.

Leave a comment

Your email address will not be published. Required fields are marked *