JavaScript: Comments
In addition to code, source files can contain comments. This is text that is not part of the program and is used by programmers for notes. They are used to add explanations of how the code works, what bugs need to be fixed here, or as reminders to add something later.
// Remove the line below after implementing the registration task
console.log(10);There are two types of comments in JavaScript:
Single-line comments
Single-line comments start with //. After these two characters, any text can follow; the entire line will not be analyzed or executed.
A comment can take up the whole line. If one line is not enough, several comments are created:
// For Winterfell!
// For Lanisters!A comment can be placed on a line after some code:
console.log('I am the King'); // For Lannisters!Multi-line comments
Multi-line comments start with /* and end with */.
/*
The night is dark and
full of terrors.
*/
console.log('I am the King');Such comments are usually used for documenting code, for example, functions.
Service comments
While working, you will encounter such code in our editor:
// BEGIN
// ENDBEGIN and END here are ordinary comments that do not affect the program in any way. They show where to write the code for the task.
// BEGIN
<your solution here>
// ENDWhen you see BEGIN and END, write your code between them and leave the rest unchanged.
Instructions
You are writing a program and realize that one part needs to be finished later. To avoid forgetting, programmers leave notes for themselves right in the code — TODO comments.
Add such a comment to the file:
// TODO: add a greeting functionWhen you come back to this place later, the comment will remind you that there is still unfinished work here.
Tips
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
Your exercise will be checked with these tests:
// @ts-nocheck -- tsconfig has no Node.js types (types: []), and the test reads the source via node:fs
import { readFileSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import { expect, test } from 'vitest';
test('comments', () => {
const code = readFileSync(
fileURLToPath(new URL('./index.js', import.meta.url)),
'utf-8',
);
// The code must contain at least one single-line TODO comment
expect(code).toMatch(/\/\/\s*TODO/i);
});Teacher's solution will be available in:
20:00
