Javascript Comments
📣 Sponsor
Throughout all of our tutorials and articles, we use comments extensively in our code examples. Commenting lets us add descriptive text to our code so we can remember or tell others why we did what we did. There are a few ways to write comments in Javascript.
How to comment code
Below is an example of how to comment your code:
// Anything written after a double slash is a commenting
// It will be ignored by the Javascript parser, so it lets us add details to why we did what we did.
// The below line is not commented, so will be parsed.
let i = 1;
Another way to comment..
We can also use /*
and */
:
/*
This is a comment block
Everything within this is a comment
And can be safely ignored by Javascript
*/
let i = 1;
Why is commenting important?
When we write code, we know exactly why we are doing things, but upon looking at it again, it can be quite confusing why we did anything at all. Comments give context not only to us, but anyone else who picks up this code in the future. That let's everyone work better together, and means your code is much easier to understand.
More Tips and Tricks for Javascript
- Javascript Temporal and How it Works
- The Free Course for Javascript
- Removing the last element of an array in Javascript
- Demystifying how 'this' works in Javascript
- How to get the current URL with Javascript
- Javascript: Check if an Array is a Subset of Another Array
- Generating a Sitemap with Node.JS and Javascript Automatically
- How to fix 'Uncaught SyntaxError: Cannot use import statement outside a module'
- Javascript Immediately invoked function expressions (IIFE)
- Javascript Arrays - How to Remove Duplicate Elements