Skip to main content

Comments

@serverSerrverlesskiy

What are comments?#

JavaScript has several ways to specify comments in code 📟

Sometimes, when writing 🖊️ the code, you will need to provide explanations that are intended only for humans а and not for the computer. Perhaps it will be some kind of message to yourself from the past (to the Future Me!) About this or that thing in the program code. Or maybe it will just be an indication that you need to run only such and such a part of the code, and the rest should be omitted. In short, in any such case, you will need to resort to the help of comments. Comments 🗣️ are used to add ➕ hints, notes 🔖, suggestions or warnings. They can make the code easier to read and understand. They can also be used to prevent 🛑 code execution (this is a common practice when debugging code).

JavaScript has several ways to specify comments 🗣️ in code.

Single line comment#

Slash

The first 1️⃣ way is // comment 🗣️ in this case all text 📜 in the line after the double slash will be a comment 🗣️. When the interpreter sees two 2️⃣ slashes in a row //, it immediately understands that everything that goes further to the end of the line can be safely ignored. For now, ignore the syntax 📖 of the code, we will get to know it in detail a little later.

Live Editor
Result
SyntaxError: Unexpected token (1:8)
1 : return ()
            ^

Multi-line comment#

Coments

The second 2️⃣ way is / * * /, which is more flexible than the first 1️⃣. As soon as the interpreter sees / *, it thinks: "Yeah, so until I see* /, I can not pay any attention to anything here!" For example, you can use it in one 1️⃣ line 👇:

Live Editor
Result
SyntaxError: Unexpected token (1:8)
1 : return ()
            ^

Alternatively, you can make a multi-line comment 🗣️ as shown here 👇:

Live Editor
Result
SyntaxError: Unexpected token (1:8)
1 : return ()
            ^

Comment in the middle of the code#

Among

Also, if you wish, you can use this commenting 🗣️ in the middle of a line of code. Although this may impair the readability of the code👇:

Live Editor
Result
SyntaxError: Unexpected token (1:8)
1 : return ()
            ^

However, there are ways to reduce the need for comments. We can use certain coding techniques to refine our code simply by taking advantage of the programming language to our advantage.

This not only makes our code easier to understand, but also helps to improve the overall design of the program!

This type of code is often referred to as self-documenting.

This means that the code must be written so that, firstly, in the future, it would be convenient for other programmers to work with it, including its author, and secondly, taking into account the possibility of extending this code.

With the commenting on the code, that's all and the call for a break sounded.

Comments

Problems?#

Problem

Write to Discord chat.

Questions:#

Question

Who are the comments for?

  1. Person
  2. Console
  3. JavaScript interpreter

What are comments not used for?

  1. Adding tips, notes, suggestions
  2. As part of the program code
  3. To prevent code execution

Which way of specifying a comment in JavaScript code does not exist?

  1. ('')
  2. //
  3. / * * /

How do I disable any number of lines of code?

  1. Write a comment in the middle of the code
  2. Wrap the code in a comment
  3. Use the command console.log

What term refers to code that has useful variable names and descriptive function names so that the developer can easily understand the purpose of the code as he reads it (without much need for explanatory comments)?

  1. Self-documenting
  2. Documenting
  3. Manuscript

In order to understand how much you learned this lesson, take the test in the mobile application of our school on this topic or in our telegram bot.

Sumerian school

Links#

  1. MDN web docs
  2. Код для подростков: прекрасное руководство по программированию для начинающих, том 1: Javascript - Jeremy Moritz
  3. JavaScript.ru

Contributors ✨#

Thanks goes to these wonderful people (emoji key):


Dmitriy Vasilev

📖

Resoner2005

🐛 🎨 🖋

Become a Patron!