Watch here: https://youtu.be/63fHgi2lvc0
Introduction
Programming languages offer the option of leaving notes for yourself or others. The JavaScript comment feature simplifies the production of more readable code. They are easy to write and recognize.
In programming, our first consideration is usually the machine — how the computer is reading and interpreting the code we write. However, it’s equally important to consider the people who will be reading and working with the code. Whether you’re working with a team or on your own, you will need to learn to properly comment and structure your code for human readers.
Comments are annotations in the source code of a program that are ignored by the interpreter, and therefore have no effect on the actual output of the code. Comments can be immensely helpful in explaining the intent of what your code is or should be doing.
As a developer, it can be frustrating to delve into code written by someone else that was not properly commented on, and it’s remarkably easy to forget what your own code meant when you’re no longer immersed in the context of a program. Commenting on your code early on will reinforce good programming habits throughout your career to avoid these issues later on.
Benefits of Javascript comments:-
- Comments in JavaScript are used to explain the code and make the program more readable for developers.
- In programming, comments can also be used to prevent some code lines from being executed. This is useful when testing.
- There are single-line comments (which comment out one line or a part of one line) and multi-line comments (which comment out a block of code).
- Multi-line comments are more often used for formal documentation.
Comment Syntax
two different types of JavaScript comment syntax.
Single-line comments are written with two forward slashes (//
):
// This is a comment
All characters immediately following the //
syntax until the end of the line will be ignored by JavaScript.
Block comments, sometimes referred to as mutli-line comments, are written with opening tags (/*
) and closing tags (*/
). If you know CSS, then you’re already familiar with block-level comments.
Everything between the opening and closing tag in the code block above will be ignored.
Inline Comments
Single-line comments are referred to as inline comments when they appear at the end of a line of code.
let a = 99; // assign numerical value to a
let b = a + 2; // assign the sum of a + 2 to b
Inline comments can be used for quick annotation on small, specific snippets of content. Since the comment should only relate to the exact line it’s written on, it is the most obvious type of comment.
Remember that there is no way to end a single line comment on a line, so make sure not to put any code after the //
syntax, as seen in the example below.
for (let v = 0; v === 100; i++) // for loop that runs 100 times {
// Running this code results in a syntax error
}
Block Comments
Block-level comments, or multi-line comments, are long-form annotations used to introduce and explain a section of code. Often these types of comments are placed at the top of a file, or before a particularly complex code block.
/* Initialize and invoke a the isRahulGandhiOk function
to assign user's name to a constant and check out
he is ok or not. */
function isRahulGandhiOk() {
const name = "@RAHUL"
console.log("Hello ," + name + "! How are you?");
}
isRahulGandhiOk();
You may also sometimes see a slightly modified version of the block comment syntax, which starts with /**
and includes asterisks throughout the left side of comment block.
Sometimes this type of comment will also include details about the programming file, including the script’s name, version, and author.
javascript comments,javascript tutorial,comments in javascript,javascript,javascript comments example,web design tutorial,js comments,javascript with mukund,mukund programming tutorials,javascript comment,npm jsdoc,jsdoc,how to create documentation in jsdoc,how to use ** in javascript,how to use ** in js comment,annotation in javascript,javascript tutorials,javascript tutorial for beginners
Comments
Post a Comment