JavaScript Statements, Semicolons, White Space and Comments

JavaScript statements are composed of: 
  • Values, 
  • Operators, 
  • Expressions, 
  • Keywords and
  • Comments.
The following statement tells the browser to write "Hello JavaScript." inside an HTML element with id="example":
Example:
<!DOCTYPE html> 
<html> 
<body> 
<h2>JavaScript Statement Example</h2> 
<p>In this Example, JavaScript statements are executed by the browser.</p> 
<p id="demo"></p> 
<script> 
document.getElementById("example").innerHTML = "Hello JavaScript."; 
</script> 
</body> 
</html>
Most JavaScript programs contain many JavaScript statements. 
Note: JavaScript programs (and JavaScript statements) are often called JavaScript code.

Semicolons ( ; ):
Semicolons separate JavaScript statements. You should add a semicolon at the end of each executable statement.
Example:
<!DOCTYPE html> 
<html> 
<body> 
<h2>JavaScript Semicolons</h2> 
<p>JavaScript Semicolons separates JavaScript Statemant.</p> 
<p id="example"></p> 
<script> 
var x, y, z; 
x = 10; 
y = 12; 
z = x + y; 
document.getElementById("example").innerHTML = z; 
</script> 
</body> 
</html>
Multiple statements on one line are allowed, When separated by semicolons:
Example:
<!DOCTYPE html> 
<html> 
<body> 
<h2>JavaScript Semicolons</h2> 
<p>JavaScript Semicolons separates JavaScript Statemant.</p> 
<p id="example"></p> 
<script> 
var x, y, z; 
x = 11; y = 13; z = x + y; 
document.getElementById("example").innerHTML = z; 
</script> 
</body> 
</html>
JavaScript White Space:
JavaScript ignores multiple spaces. To make it more readable just add white space to your script. The following lines are equivalent:
var person = "amit"; 
var person="amit";
Put spaces around operators ( = + - * / ) is a good practice.
Example:
var a = b + c;
var person = "amit";
JavaScript Comments:
To make Code more readable and explain JavaScript code use javascript comments.

Two Types of Comments in JavaScript:
  1. Single Line Comments
  2. Multi-line Comments
Single Line Comments:
Single line comments start with //. 
Javascript can not execute the comments line
Example of single line comment:
<!DOCTYPE html> 
<html> 
<body> 
<h1 id="id1"></h1> 
<p id="id2"></p> 
<script> 
// Change heading 
document.getElementById("id1").innerHTML = "JavaScript Single Line Comments"; 
// Change paragraph 
document.getElementById("id2").innerHTML = "A Simple paragraph."; 
</script> 
</body> 
</html>
Example of single line comment:
<!DOCTYPE html> 
<html> 
<body> 
<h2>JavaScript Single Line Comments</h2> 
<p id="example"></p> 
<script> 
var a = 10;    // Declare a, give it the value of 10 
var b = a + 2;  // Declare b, give it the value of a + 2
document.getElementById("example").innerHTML = b; // Write b to example
</script> 
</body> 
</html>
Multi-line Comments:
It start with /* and end with */. 
Ignored by JavaScript if any text between /* and */ will be. 
Example of Multi line comment:
<!DOCTYPE html> 
<html> 
<body> 
<h1 id="heading1"></h1> 
<p id="paragraph1"></p> 
<script> 
/* The code below will change 
the heading with id = "heading1
and the paragraph with id = "paragraph1" */ 
document.getElementById("heading1").innerHTML = "JavaScript Multi Line Comments"; 
document.getElementById("paragraph1").innerHTML = "A Simple paragraph."; </script> 
</body> 
</html>
Note: Multi Line Comments are often used for formal documentation.
Project Source Code:
<!DOCTYPE html> 
<html> 
<body> 
<h2>JavaScript Statements, Semicolons, White Space and Comments</h2> 
<p id="example"></p> 
</body> 
</html> 
<script type="text/javascript"> 
var x, y, z; // that is a statement 
x = 5; // that is also statement 
y = 6; // that is also statement 
z = x + y; //  ; samicolon 
// - single line comment 
/*-multi line comment*/ 
// ; semicolon 

alert(z);
</script>


EmoticonEmoticon