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.
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.
Создавать обучающие материалы, понятные для всех без исключения, довольно сложно. Мы очень стараемся, но всегда есть что улучшать. Если вы встретили материал, который вам непонятен, опишите проблему в обратной связи нашего сообщества
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
