JavaScript Comments to Prevent Execution

We can use // or /*…*/ to change the JavaScript code execution using comments. JavaScript Comments are used to prevent code execution and are considered suitable for testing the code.

Example 1: JavaScript comments are used to prevent execution of selected code to locate problems of code or while testing new features. This example illustrates that commented code will never execute. 

Javascript
function add() {
    let x = 10;
    let y = 20;
    let z = x + y;
    // console.log(x + y);
    console.log(z);
}

add();

Output
30

Example 2: This example uses multi-line comments to prevent the execution of addition code and perform subtraction operations.

Javascript
function sub() {
    let x = 10;
    let y = 20;
    /* let z = x + y;
    console.log(z); */

    let z = x - y;
    console.log(z);
}

sub();

Output
-10

JavaScript Comments

JavaScript comments help explain code, making it easier to understand. You can also use them to temporarily disable parts of your code. The JavaScript compiler ignores comments when running the code.

Similar Reads

1. Single Line Comments

A single-line comment in JavaScript is denoted by two forward slashes (//),...

2. Multi-line Comments

A multiline comment in JavaScript is a way to include comments that span multiple lines in the source code....

JavaScript Comments to Prevent Execution

We can use // or /*…*/ to change the JavaScript code execution using comments. JavaScript Comments are used to prevent code execution and are considered suitable for testing the code....

Contact Us