In this blog post, you'll learn how to write comments in JavaScript. Comments are an important part of any programming language because they make code more readable and understandable. They help you and other developers better understand and maintain the code.
Single-line comments
Single-line comments in JavaScript begin with two slashes //
Anything after the slashes on the same line is ignored by the JavaScript interpreter and not executed.[2][5] Here's an example:
// Dies ist ein einzeiliger Kommentar console.log("Hallo Welt!"); // Dieser Kommentar erklärt, was die folgende Zeile tut
Multi-line comments
Multi-line comments in JavaScript begin with /*
and end with */
Anything between these two characters is ignored by the JavaScript interpreter. Here's an example:
/* Dies ist ein mehrzeiliger Kommentar. Du kannst hier mehrere Zeilen Text schreiben, um deinen Code ausführlicher zu erklären. */ console.log("Hallo Welt!");
Why use comments?
Comments are useful for explaining the purpose and functionality of sections of code. They make websites, web applications, and programs easier to understand and maintain. Comments can also be used to temporarily disable certain sections of code by "commenting them out." This is helpful when you want to test parts of your code or isolate errors.
Summary
In JavaScript, there are two types of comments: single-line comments and multi-line comments. Single-line comments begin with //
, while multi-line comments with /*
start and with */
Comments are important for making code understandable and maintainable, and they can also be used for testing and debugging.
Here is an example that uses both single-line and multi-line comments:
// Funktion, die eine Begrüßung ausgibt function begruessung() { /* Dieser mehrzeilige Kommentar erklärt, dass die Funktion eine Begrüßungsnachricht in der Konsole ausgibt. */ console.log("Hallo Welt!"); } begruessung(); // Aufruf der Funktion
Have fun commenting your JavaScript code!