JavaScript Comments and Statements

JavaScript Comments and Statements

In this article, I am going to discuss JavaScript Comments and Statements with examples. Please read our previous article, where we discussed what are the different places to place the JavaScript code in an HTML File. As part of this article, we are going to discuss the following pointers.

  1. What are Statements in JavaScript?
  2. Is JavaScript Case Sensitive?
  3. Understanding the Whitespaces and Line breaks in JavaScript.
  4. JavaScript Blocks:
  5. JavaScript comments.
  6. Advantages of JavaScript comments:
  7. Types of JavaScript comments.
What are Statements in JavaScript?

JavaScript is a collection of statements and these statements are executed by the browser. Each statement is run or executed by the browser in the order they are written. The below example will write a heading and paragraph to a web page:

<script language="javascript" type="text/javascript">
 document.write("<h1>This is heading</h1>");
 document.write("<p>This is paragraph</p>");
</script>

JavaScript Statements

JavaScript is Case Sensitive:

Unlink HTML, JavaScript is a case-sensitive language. That means we need to properly watch our capitalization of letters when we write JavaScript statements, create or call variables, objects, and functions.

For example time, Time and TIME will have different meanings in JavaScript.

Whitespaces and Line breaks:

JavaScript ignores spaces, tabs, and newlines that get created while writing a JavaScript program. Since these are ignored by JavaScript, we are free to format our program in a neat and consistent way that makes the code easy to read and understand.

var name = “JavaScript”;
var name=”JavaScript”;

If JavaScript code doesn’t fit in one line, we can break the line. And the best place to break the line is after an operator

document.getElementById(“demo”).innerHTML =
“Hello Shagufta!”;

The Semicolon (;) is Optional:

A semicolon is an option according to the JavaScript standard and the browser is supposed to translate the end of the line at the end of the statement. Because of this, we will often see programs without the semicolon at the end.

<script language=”javascript” type=”text/javascript”>
var1 = 5
var2 = 10
</script>

Use semicolons if we want to write multiple statements on one line.

<script language=”javascript” type=”text/javascript”>
var1 = 5; var2 = 10;
</script>

Note: It’s a good programming practice to use a semicolon.

JavaScript Blocks:

A group of JavaScript statements is enclosed by braces {} called a block. Block starts with a left curly bracket { and ends with a right curly bracket }. The purpose of a block is to make the execution of statements together in a sequence/order.

Below is the example:

<script language=”javascript” type=”text/javascript”>
{
       document.write(“<h1>This is heading</h1>”);
       document.write(“<p>This is paragraph</p>”);
}
</script>

JavaScript Comments:

The Comment is used to add information about the code, explain the JavaScript code so that end-user can easily understand the code. The JavaScript comment is ignored by the JavaScript engine.

Advantages of JavaScript comments:
  1. It is used to explain briefly the code so that the end-user can easily understand the code.
  2. It is used to avoid the code from execution, later which is not needed in a program anymore.
Types of JavaScript Comments:

There are two types of comments:

  1. Single-line comment
  2. Multi-line comment
Single-line comments:

Single-line comments start with //. Any statement starts with // is treated as a comment and ignored by JavaScript. This means it won’t be executed.

Example:

<script language=”javascript” type=”text/javascript”>
{
     //Write a headig
     document.write(“<h1>This is heading</h1>”);
     document.write(“<p>This is paragraph</p>”);//Write a paragraph
}
</script>

Multi-line comments:

Multi-line comments start with /* and end with */. Any statement between /* and */ is treated as a comment and ignored by JavaScript. This means it won’t be executed. It can be used to add a single line and multi-line comments also.

Example:

<script language=”javascript” type=”text/javascript”>
{

/*
write one heading and
one paragraph
*/

document.write(“<h1>This is heading</h1>”);
document.write(“<p>This is paragraph</p>”);

}
</script>

In the next article, I am going to discuss JavaScript Variables in detail. Here, in this article, I try to explain JavaScript comments and statements in detail. I hope you enjoy this article.

Leave a Reply

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