Back to: JavaScript Tutorial For Beginners and Professionals
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.
- What are Statements in JavaScript?
- Is JavaScript Case Sensitive?
- Understanding the Whitespaces and Line breaks in JavaScript.
- JavaScript Blocks:
- JavaScript comments.
- Advantages of JavaScript comments:
- 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 it is 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>
Is JavaScript Case Sensitive?
Yes, JavaScript is a case-sensitive language. Variable names, methods, keywords, object properties, and event handlers all are case-sensitive. So, you 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 the end-user can easily understand the code. The JavaScript comment is ignored by the JavaScript engine.
Advantages of JavaScript comments:
- It is used to explain briefly the code so that the end-user can easily understand the code.
- 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:
- Single-line comment
- Multi-line comment
Single-line comments:
Single-line comments start with //. Any statement that 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 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 JavaScript comments and statements article.
Hi team along with these tutorials we required react ,azure, docker courses
you can ask them for more
For example, a variable named “count” is different from a variable named “Count”. If you use the wrong case when referring to a variable or function in JavaScript, it will result in an error