JavaScript Comments Tutorial with Examples


Comments are used to explain code and make it more readable by others in JavaScript programming language. Comments can be a single line, multiline or inline. Another useful usage of the comments is creating testing code and executing or preventing the execution of the testing code.

Single-Line Comments

Single line comments only comment on the used line. Double slash // is used to create the single-line comment and from the position // to the end of the line will be marked as a comment and it is omitted by the browser. But the commented line is readable by others even it is not executed and omitted. Also, most of the editors will make the comments a different color like green which is not the same as the JavaScript code.

// This is a single line comment

alert("This is a JavaScript alert which is working");

// THIS IS A SINGLE LINE COMMENT

alert("This is a JavaScript alert which is working");

// 123456789 This is a comment too.

alert("This is a JavaScript alert which is working");

Single Line Inline Comments

We can use the single-line comment as an inline comment in the single line. An inline comment is a comment which is added to the code line where the code will be executed but the comment will not execute. From the start of the inline comment to the end of the line will be a comment. But from the start of the line to the start of the inline comment will be a code line that will be executed.

var age = 12;  // This is single line inline comment and the new variable age will be created

var new_age = age + 20; // This single line inline comment will not affect to sum of age with 20

Multi-line Comments

Multi-line comments can be used to put more comments or explanations about the code. Multi-line comments also used to provide more details about the function, classes, etc. In order to create a multi-line comment, we should set the start and end of the multiline comment. /* is used to set the start f the multi-line comment and */ is used to set the end of the multi-line comment. Between /* and */ are interpreted as a comment.

/* This is
Multi line comment
*/

alert("Test");

/*
This 
is 
Multi-line
Comment
Too
*/

Prevent Code Execution with Single Line Comments

While testing some JavaScript code single line comment can be used to test some code and then prevent the execution of the given code line. In the following example, we will prevent the alert() function to print screen Hi which is done by the single-line comment. We can enable this code line just by uncommenting the code line.

// alert("Hi");

Prevent Code Execution with Multi-Line Comments

We can also use multi-line comments in order to prevent some code from execution. This can be useful for more complex test scenarios. In the following example, we will comment on multiple line codes to prevent the execution.

/*

alert("Hi");

callThisFunction();

*/

alert("Merhaba");

callThatFunction();

In this example the alert("Merhaba"); and callThatFunction() will be executed properly where alert("Hi") and callThisFunction() will not executed because they are commented or just comments.

LEARN MORE  C Syntax and Basics

Leave a Comment