Writing Comment Lines in JavaScript
Login
The first thing we usually think about when programming is how the computer reads and executes the code we write.
But there is something else that is just as important: people.
It is very important to make your code understandable to those who will read or work on it after you.
Whether you're working with a team or on your own, correctly commenting and editing your code is essential for human readers.
Comment lines are comments added to a program's source code and ignored by the interpreter.
So they do not affect the output of the code. But they are very useful in explaining what the code does or should do.
As a developer, it can be really frustrating trying to understand code written but uncommented by someone else.
Likewise, after a while it is very easy to forget what the code you wrote means.
Adding comments to your code at an early stage creates a good habit that will help you throughout your career and prevents such problems.
Comment Syntax
There are two different ways to write comment lines in JavaScript.
One-line comments begin with a double slash //:
// Bu bir yorum satırıdır.
In comments starting with //, all characters written until the end of the line are ignored by JavaScript.
Block comments are used for multi-line comments.
It starts with /* and ends with */.
If you have CSS knowledge, you should already be familiar with this structure.
/* Bu bir
yorum satırıdır */
Everything between the opening and closing tags is ignored by JavaScript.
Both single-line and multi-line comments are often written above the code they comment.
Like the Welcome to GenixNode Doc..” example below:
// Konsola "GenixNode Doc'a Hoşgeldiniz.." yazalım.
console.log("GenixNode Doc'a Hoşgeldiniz..");
When writing comments, indent them at the same level as the code immediately below:
// Bir fonksiyonu başlat
function alphabetizeOceans() {
// oceans değişkenini stringlerden oluşan bir liste olarak tanımla
const oceans = ["Pasifik", "Atlantik", "Hint", "Antarktik", "Arktik"];
// Alfabetik sıralanmış diziyi konsola yazdır
console.log(oceans.sort());
}
Don't forget, comment lines are also part of the program.
Unupdated (old) comments can be even more harmful than no comments at all.
That's why it's important to regularly update your comments along with your code.
Inline Comments
Single-line comments are called inline comments when written at the end of a line of code.
let x = 99; // x değişkenine sayısal değer ata
let y = x + 2; // x + 2 işleminin sonucunu y'ye ata
Inline comments are useful for quick clarification for small and specific pieces of code.
The comment should only be related to the line it is on, so it is the clearest and clearest type of comment.
Remember: For single-line comments (//), everything up to the end of the line is considered a comment.
That means you cannot write code on the same line after typing //. Like the example below:
for (let i = 0; i === 10; i++) // 10 kez çalışacak for döngüsü
{
// Bu kodu çalıştırmak bir sözdizimi (syntax) hatası verir
}
While inline comments can be useful, they should be used in moderation.
Adding too many comments next to each line will quickly make your code complex and difficult to read.
Block Comments
Block comments (multi-line comments) are long comments used to introduce or explain a section of code.
Often such comments are placed at the top of the file or in front of a particularly complex block of code.
/* greetUser fonksiyonunu başlat ve çalıştır.
Kullanıcının adını bir sabite ata
ve bir karşılama mesajı yazdır. */
function greetUser() {
const name = prompt("Adınız nedir?");
console.log("Merhaba, " + name + "! Nasılsın?");
}
greetUser();
Sometimes you may also see a slightly modified version of the block comment syntax.
Such comments begin with /** and have asterisks along the line on the left side of the comment block.
/**
* Bir dizi string sabiti başlat.
* Dizideki her öğeyi döngüyle gez
* ve konsola yazdır.
*/
const seaCreatures = ["Köpekbalığı", "Balık", "Ahtapot"];
for (const seaCreature of seaCreatures) {
console.log(seaCreature);
}
Sometimes such comments also contain additional information about the file.
For example, details such as the name of the script, its version or author information can be written here.
If you're new to JavaScript, you can write as long comments as necessary to understand the code you're writing.
However, as you improve over time, it will be more useful to focus on explaining the purpose (why) of the code in the comments, rather than asking "how" or "what" questions.
Code Commenting for Testing Purposes
Comment lines can also be used to quickly prevent the code you wrote from executing.
This is called "commenting out code"**.
If there is an error in the code you wrote, you can stop it from running by commenting certain sections.
This method helps find the source of the problem.
It can also be used to switch between blocks of code to test different results.
// İki sayıyı toplayan fonksiyon
function addTwoNumbers(x, y) {
let sum = x + y;
return sum;
}
// İki sayıyı çarpan fonksiyon
function multiplyTwoNumbers(x, y) {
let product = x * y;
return product;
}
/* Bu örnekte addTwoNumbers fonksiyonunu yorum satırına aldık.
Bu yüzden çalışmayacak. Sadece multiplyTwoNumbers fonksiyonu çalışacak. */
// addTwoNumbers(3, 5);
multiplyTwoNumbers(5, 9);
You can use both single-line comments and block comments to comment out your code.
Which method you choose depends on the size of the section you want to interpret.
Note: Commenting your code should only be done for testing purposes.
Do not leave code fragments in the comment line in the file you will deliver or publish in its final form.
When trying to understand the logic of a program, commenting the code can be very useful.
With this method you can find where the errors are or evaluate which lines of code actually work.
Result
JavaScript code is executed by the computer, but it is always read by other programmers. Adding correct comments, especially to complex sections, will benefit you greatly in the future.
This way, both you and your teammates can more easily understand the purpose of the code you write.

