JavaScript Basics Part 2: Comments

JavaScript comments are a way to keep your code clean and easy to read. Comments cannot be rendered because the browsers skip them. Most web designers are probably familiar with HTML comments, which are similar to JavaScript comments with the exception of the syntax.

There are two types of comments that you can use in JavaScript: single and multiple line comments. Single line comments are created using two slashes “//” in front of the line of text. Multiple line comment blocks are created by wrapping the text with a slash and an asterisk “/*” and closing the comment block by adding an asterisk and a slash “*/”. In CSS, comments are setup in a similar way with multiple line blocks.

Single line comments:

// This is my first JavaScript Program
document.write(‘This is very cool stuff’);

Multiple line comment block:

/* This is my comment in JavaScript
which spans multiple lines */

Comments are a great way to document what your program is doing, but it’s important not to go overboard when adding comments, as they can add unnecessary bloat to your code. Comments will help other designers and developers understand your code. When distributing your code to the open source community, or to those who may not fully understand the JavaScript alone, comments can be very helpful.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: