JavaScript 6

JavaScript 6

Chapter 6 with Examples

JavaScript Comments

JS comments are used to make programming easier to read and understand. Comments are not executed by the browser. Comments can be used to add notes, suggestions and hints to a JS code. There are two types of comments in JS, the single-line and the multi-line.

Single-Line Comments

To make a single-line comment, add two forward slash// to the beginning of the line. This makes all text following it to be a comment. Here is an example:

// this function changes the content of the paragraph element
function myFunc(){
var text = "Hello World!";
document.getElementById("demo").innerHTML = text;
}

Another way is to use the /* */, the comment goes between the asterisks. This technique is much more flexible. Here is an example:

/*this function changes the content of the paragraph element*/
function myFunc(){
var text = "Hello World!";
document.getElementById("demo").innerHTML = text;
}

Multi-Line Comments

To make multi-line comments, use this syntax:

/*
your multi-line comments
your multi-line comments
your multi-line comments
*/
function myFunc(){
var x = 6;
var y =9;
var sum = x + y;
/* let's add the value of y to x 
and then print the sum*/
/*
you can also comment like this
sometimes it's more convenient
and easier to see
*/
document.getElementById("demo").innerHTML = sum;
}

Disabling Code Using Comments

We can disable codes to prevent it from running using comments. This technique is helpful for testing purposes. In the example below, only the product will be written because the writing of the sum will not be executed.

function myFunc(){
var x = 6;
var y =9;
var sum = x + y; //adds x and y
var product = x * y; // multiplies y to x
document.getElementById("demo").innerHTML = product;
  // document.getElementById("demo").innerHTML = sum;
}