JavaScript Comments JAVASCRIPT
- JavaScript Introduction
- JavaScript Syntax
- JavaScript innerHTML
- JavaScript document.write()
- Javascript - window.alert()
- JavaScript - console.log()
- JavaScript Comments
- JavaScript Variables
- JavaScript Operators
- JavaScript Data Types
- JavaScript Functions
- JavaScript Objects
- JavaScript Events
- JavaScript Strings
- JavaScript String Methods
- JavaScript Numbers
- JavaScript Number Methods
- JavaScript Arrays
- JavaScript Array Methods
- JavaScript Sorting Arrays
- JavaScript Array Iteration
- JavaScript Date Objects
- JavaScript Date Formats
- JavaScript Get Date Methods
- JavaScript Set Date Methods
- JavaScript Math Object
- JavaScript Conditions
- JavaScript Switch
- JavaScript Loop For
- JavaScript While Loop
- JavaScript Break and Continue
- JavaScript Type Conversion
- JavaScript Errors
- JavaScript Scope
- JavaScript this Keyword
- JavaScript Classes
- JavaScript Debugging
- JavaScript - Changing CSS
- JavaScript JSON
JavaScript Comments
JavaScript Comments
JavaScript comments can be used to explain JavaScript code, and to make it more readable.
Single-Line Comments
Single line comments start with //
.
Any text between //
and the end of the line will be ignored by JavaScript (will not be executed).
document.getElementById("myPageHead").innerHTML = "My First Page";
// Change paragraph:
document.getElementById("myPara").innerHTML = "My first paragraph.";
This example uses a single line comment at the end of each line to explain the code:
var y = x + 6; // Declare y, give it the value of x + 6
Example
// Change heading:
document.getElementById("myPageHead").innerHTML = "My First Page";or
var x = 3; // Declare x, give it the value of 3
Multi-line Comments
Multi-line comments start with /*
and end with */
.
Any text between /*
and */
will be ignored by JavaScript.
Example
/*
The code below will change
the heading with id = "myPageHead"
and the paragraph with id = "myPara"
*/
document.getElementById("myPageHead").innerHTML = "JavaScript Comments";
document.getElementById("myPara").innerHTML = "My first paragraph.";
Using Comments to Prevent Execution
Adding //
in front of code, line changes the code lines from an executable line to comment.
Example
<!DOCTYPE html>
<html>
<body><h2>JavaScript Comments</h2>
<h1 id="myPageHead"></h1>
<p id="myPara"></p>
<script>
//document.getElementById("myPageHead").innerHTML = "JavaScript Comments";
document.getElementById("myPara").innerHTML = "My first paragraph.";
</script><p>The line starting with // is not executed.</p>
</body>
</html>