How to comment your code | JavaScript essentials

/ #JavaScript


Commenting your code is important for readability and maintainability. Here's a couple of ways to add comments to your JavaScript code.

Many people think that commenting is a waist of time. Espacially when you're the only one reading your code. But 6 months from now, it might not be as easy to remember why you solved something a special way as you think.

Comment your code and save time!

Single line comments

I think the type of JavaScript comments I use most is the single line comments. Here's a couple of examples:

// Set variables
const a = 'a'
let b = 'b'

// Change variable
b = 'changed to d'

// Log
console.log('b:', b)

Another typical use of the single-line comment is to use it prevent a line of code from running.

a = 1
b = 2
//console.log(a)

If you run this code, the line with console.log will not be executed.

Multi-line comments

Comments can also span over multiple lines. Check this example:

/*
This function will take to parameters
and combine them.
*/
function combine(a, b) {
	return a + b
}

Multi-line comments makes it easy to add a good description of a function or a part of your code.

Comments

wlkrlsucnx | Mar 25, 21 11:15

Muchas gracias. ?Como puedo iniciar sesion?

Add comment

Info

Please log in to comment!

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.