Skip to main content

How and When to Write Comments in Javascript?

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.

/* This is
a comment */

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.

/**
* Initialize constant with an array of strings.
* Loop through each item in the array and print
* it to the console.
*/


const seaCreatures = ["Shark", "Fish", "Octopus"];

for (const seaCreature of seaCreatures) {
console.log(seaCreature);
}
 

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

Popular posts from this blog

Top 10 topics to make YouTube video in angular

  Here are ten topic suggestions for making YouTube videos on Angular: Getting Started with Angular: An Introduction to the Angular Framework Components in Angular: Understanding and Creating Angular Components Directives in Angular: Understanding and Using Angular Directives Angular Routing: Setting up Routes and Navigation in Angular Applications Forms in Angular: Creating and Validating Forms with Angular Reactive Forms Pipes in Angular: Understanding and Using Angular Pipes to Transform Data Services in Angular: Creating and Using Angular Services for Data Management Angular Animations: Adding Animations to Angular Applications Angular Material: Implementing Material Design in Angular Applications Advanced Angular Topics: Exploring Advanced Angular Topics like Lazy Loading, AOT Compilation, and Dynamic Components.

Top 5 javascript frameworks

  Here are the top 5 JavaScript frameworks : React: Developed and maintained by Facebook, React is a popular JavaScript library for building user interfaces. Angular: Developed and maintained by Google, Angular is a complete framework for building dynamic web applications. Vue.js: A relatively new JavaScript framework, Vue.js has gained a lot of popularity in recent years for its simplicity and performance. Ember.js: Ember.js is a JavaScript framework that emphasizes conventions over configuration, making it a good choice for complex web applications. Backbone.js: Backbone.js is a lightweight JavaScript framework that provides structure for web applications, particularly those that need to work with a lot of data. It's worth noting that the popularity of these frameworks can change over time, and different frameworks may be better suited for different types of projects and use cases.