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

How to get the CPU temperature using ADB?

import os # Function to get the CPU temperature using ADB def get_cpu_temperature():     try:         # Run the ADB command and redirect the output to a temporary file         os.system("adb shell cat /sys/class/thermal/thermal_zone0/temp > temp.txt")         # Read the temperature from the temporary file and convert to Celsius         with open("temp.txt", "r") as file:             temperature = int(file.read().strip()) / 1000.0             return temperature     except Exception as e:         print("Error:", e)         return None     finally:         # Remove the temporary file         os.remove("temp.txt") # Function to check temperature and trigger alarm if necessary def check_temperature_and_notify(threshold):     temperat...

How to call API in angular & with an example of PHP API

How to call API in angular & with an example of PHP API . Software required:- Node ->  https://nodejs.org/en/download/ VS Code ->  https://code.visualstudio.com/download Angular CLI -> npm install - g @angular / cli To understand how to call API in angular follow the below steps:- Step:1 Project Setup 1. Create a project in angular ->  ng new project name 2. Install npm -> npm i 3. Import HttpClientModule in app.module.ts -> import { HttpClientModule } from '@angular/common/http' ; 4. Add provider providers : [ WebserviceService ], example -> ng new crudInAngularPHP Step:2 API Service 1. Create a service with name webservice -> ng g s services/webservice 2. import modules import { HttpClient, HttpHeaders, HttpRequest, HttpEvent } from '@angular/common/http'; 3 .    C all constructor  constructor( private http: HttpClient) 4. Add a common function to get a response of API with name postRequest:-      ...

What is JavaScript? Why JavaScript? How to use javascript?

 WWH JavaScript Watch here with full example and with github:  Watch Now 1. Question. What is JavaScript? Answer :- Javascript is a scripting language. And it's basically used to make web pages more user-friendly and more interactive.     Few Important points related to javascript:- JavaScript is an untyped language.(What does it mean?) Unlike c/java or other programming languages, you don't need to define/declare type of variables JavaScript is an interpreted language.(Means?) Programs written in c/c+ or java, you need to compile first and after that, u can get output. but codes/programs written in javascript will give you output without compilation process. 2. Question. Why JavaScript?  Answer:- Javascript is basically used to make web pages more authentic. For example, you have a signup form of a user and there is a Pincode input box and you want to get the city on the input of pin code so you can add this functionality into your web page with the help of jav...