Skip to main content

Command Palette

Search for a command to run...

JavaScript 6

Chapter 6 with Examples

Updated
2 min readView as Markdown
JavaScript 6
A

I am a passionate front-end engineer with 3 years of experience in developing and implementing innovative software solutions.

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;
}

JavaScript

Part 6 of 11

This blog is all about JavaScript topics from Basics to Advance, along with interview question.

Up next

JavaScript 7

Chapter 7 with Examples

More from this blog

A

Arizon.Dev

12 posts

Hi everyone, My name is Arinze Calistus and I'm a self-taught Full-Stack developer that learned and is still learning through online resources. I would love to connect with people and have a chat.