JavaScript Tutorial
JS - Introduction JS - Basic JS - Placement JS - Output JS - Statements JS - Syntax JS - Variables JS - Comments JS - Data Types JS - Operators JS - Operator Precedence JS - Condition JS - Switch JS - Functions JS - Objects JS - Loops For JS - Loops While JS - Break JS - Strings JS - String Methods JS - Numbers JS - Number Methods JS - Arrays JS - Array Methods JS - Array Iteration JS - Events JS - Event Listener JS - Event Propagation JS - Date JS - Math JS - Random JS - Boolean JS - Type Conversion JS - RegExp JS - Exception JS - Scope JS - Debugging JS - Hoisting JS - Strict Mode JS - this Keyword JS - Mistakes JS - Best Practices JS - Performance JS - Form Validation JS - ES6 Features
JS Objects
Object Definitions Object Properties Object Methods Object Constructors Object Prototypes
JS Functions
Function Definitions Function Parameters Function Call Function Apply Function Closures
JS HTML DOM
DOM Introduction DOM Methods DOM Selectors DOM HTML DOM CSS DOM Attributes DOM Navigation
JS Browser BOM
JS - Window JS - Screen JS - Location JS - History JS - Navigator JS - Popup Alert JS - Timing JS - Cookies
JS AJAX
AJAX - Introduction AJAX - XMLHttp AJAX - Request AJAX - Response AJAX - PHP
JS JSON
JSON - Introduction JSON - Syntax JSON - Data Types JSON - Parse JSON - Stringify JSON - Objects JSON - Arrays JSON - PHP JSON - JSONP
JS References
JavaScript Reference JavaScript Methods HTML DOM Reference

JavaScript Comments

A comment is simply a line of text that is completely ignored by the JavaScript interpreter.

Comments are usually added with the purpose of providing extra information pertaining to source code.

Comments can also be used to prevent execution, when testing alternative code.


Single Line Comments

Single-line comments begin with a double forward slash (//), followed by the comment text.

Run code

When writing comments, indent them at the same level as the code immediately below them:

Run code

Single-line comments are referred to as inline comments when they appear at the end of a line of code.

Run code

Multi-line Comments

Multi-line comments or Block-level comments, are long-form annotations used to introduce and explain a section of code. Often these types of comments are placed at the top of a file, or before a particularly complex code block.

A multi-line comment begins with a slash and an asterisk (/*) and ends with an asterisk and slash (*/).

Run code

You may also sometimes see a slightly modified version of the block comment syntax, which starts with /** and includes asterisks throughout the left side of comment block.

Run code

Block comments are often used for formal documentation.


Using Comments to Prevent Execution

Comments can also be used to quickly and easily prevent execution of code for testing and debugging purposes.

Run code

Both single-line comments and multi-line comments can be used to comment out code, depending on the size of the section being toggled.

Run code

When working out the logic of a program, commenting out code can prove to be helpful as you determine where bugs are or assess the lines of code that offer the most utility.