Getting started with JavaScript

Comments - syntax and types, Semicolon

Getting started with JavaScript

In Javascript, comments are those statements which browser ignores while interpreting the code. That means essentially whatever text, character, emoji, even code you write in comment will be ignored by browser.


Syntax

So we have two variety of comments in JavaScript :

  1. Single line comment

  2. Multi line comment

A single-line comment is created when we add two forward-slash characters one after the other, without spaces.

// This is a single-line comment

And multi-line comment the commented statement is enclosed with forward-slash character with asterisk and ends with asterisk and forward-slash character.

/* This
   is
   multi-line
   comment
*/

/* We can have multi-line comment on single line */

Why writing comments is essential?

So, comments are essential because they make it possible to communicate with your future self or with your team members, allowing you to ask questions about the code, mark the code as "to do", or as "to improve", or just simply explain what a given piece of code does.

Comments also enables us write a short description about the code which helps to understand how a given piece of code works. We can also test different solutions to a coding problem, while not having to delete existing code and it also comes in handy debugging, trying to pinpoint why our code is broken or behaving unpredictably.


Is it necessary to add semicolon at the end of statement?

The browser has this interesting feature known as "Automatic Semi-Colon Insertion" meaning it pretty much fill's the missing semicolon while interpreting the code.

Which means that it's not mandatory to manually insert those semicolons, but some developer's still prefer to add it for stylistic purpose.

Personally, I prefer using it for sake of clarity.