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:

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

Multiple line comment block:
[js]
/* This is my comment in JavaScript
which spans multiple lines */
[/js]
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.

Categories: BasicsJavaScript/Ajax

0 thoughts on “JavaScript Basics Part 2: Comments”

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Basics

JavaScript Basics Part 7: Loops

Loops are used to execute the same statement of code a set number of times, which can be hardcoded or based on variable. Loops are very common within programming, as they solve the problem of Read more…

Basics

JavaScript Basics Part 6: Conditionals

Logic is a part of our everyday lives – it’s a very familiar concept, therefore one of the easiest to understand. We are faced with questions all the time, with the “If” question being the Read more…

Basics

JavaScript Basics Part 5: Arrays

An array is a variable which can store many values inside it. A variable can only store one value. Those values can be accessed at any time using the array’s index. The values stored within Read more…