(JavaScript Tutorials for Beginners)
In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript Comments.
JavaScript is a powerful and flexible programming language. It can execute on a web browser that allows us to make interactive web-pages such as popup menus, animations, form validation etc.
JavaScript has evolved a lot in the past decade. Our JavaScript tutorials will help you understand the core concepts of modern JavaScript one step at a time.
JavaScript Comments
In this tutorial, you will learn about JavaScript comments, why we use them, and how to use them with the help of examples.
JavaScript comments are hints that a programmer can add to make their code easier to read and understand. They are completely ignored by JavaScript engines.
There are two ways to add comments to code:
//
– Single Line Comments/* */
-Multi-line Comments
Single Line Comments
In JavaScript, any line that starts with //
is a comment. For example,
name = "Jack";
// printing name on the console
console.log("Hello " + name);
Here, // printing name on the console
is a comment.
You can also use single like comment like this:
name = "Jack";
console.log("Hello " + name); // printing name on the console
Multi-line Comments
In Javascript, any text between /*
and */
is also a comment. For example,
/* The following program contains the source code for a game.
For the player controlling the tiger to win, they must capture all the sheep. There are altogether 4 tigers on the board.
For the sheep to win, all tigers must be surrounded and cornered so that they cannot move. The player controlling the sheep has 20 sheep at his disposal.
*/
Since the rest of the source code will be used to implement the rules of the game, the comment above is a good example where you might use a multi-line comment.
Using Comments for Debugging
Comments can also be used to disable code to prevent it from being executed. For example,
console.log("some code");
console.log("Error code);
console.log("other code");
If you get an error while running the program, instead of removing the error-prone code, you can use comments to disable it from being executed; this can be a valuable debugging tool.
console.log("some code");
// console.log("Error code);
console.log("other code");
Pro Tip: Remember the shortcut for using comments; it can be really helpful. For most code editors, it’s Ctrl + /
for Windows and Cmd + /
for Mac.
Make Code Easier to Understand
As a JavaScript developer, you will not only write code but also may have to modify code written by other developers.
If you write comments on your code, it will be easier for you to understand the code in the future. Also, it will be easier for your fellow developers to understand the code.
As a general rule of thumb, use comments to explain why you did something rather than how you did something, and you are good.
Note: Comments shouldn’t be the substitute for a way to explain poorly written code in English. You should always write well-structured and self-explanatory code. And, then use comments.
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.
Snowflake for Beginners – Create Table With Column And Table Comments