Curriculum
Course: JavaScript Basic
Login

Curriculum

JavaScript Basic

JSHome

0/216
Text lesson

JS Comments

JavaScript comments serve multiple purposes: clarifying JavaScript code to enhance readability and understanding, and also temporarily disabling code execution for testing alternative solutions.

Single Line Comments

Single-line comments in JavaScript start with //, and any text following // until the end of the line is ignored by JavaScript and not executed. The example below shows how to use single-line comments before each line of code.

Example

// Change heading:
document.getElementById(“myH”).innerHTML = “My First Page”;

// Change paragraph:
document.getElementById(“myP”).innerHTML = “My first paragraph.”;

In this example, a single-line comment is placed at the end of each line to provide explanations for the code.

Example

let x = 5;      // Declare x, give it the value of 5
let y = x + 2;  // Declare y, give it the value of x + 2