JavaScript Statements

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>A <b>JavaScript program</b> is a list of <b>statements</b> to be executed by a computer.</p>
<p id="demo"></p>
<script>
var x, y, z;  // Statement 1
x = 5;    // Statement 2
y = 6;    // Statement 3
z = x + y;  // Statement 4
document.getElementById("demo").innerHTML =
"The value of z is " + z + ".";  
</script>
</body>
</html>

JavaScript Programs

A computer program is a list of “instructions” to be “executed” by a computer. In a programming language, these programming instructions are called statements.

A JavaScript program is a list of programming statements.

JavaScript Statements

JavaScript statements are composed of:

Values, Operators, Expressions, Keywords, and Comments.

This statement tells the browser to write “Hello Dolly.” inside an HTML element with id=”demo”:

<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>In HTML, JavaScript statements are executed by the browser. </p>
<p id="demo"></p>
<script>
document. getElementById("demo"). inner HTML = "Hello Dolly.";
</script>
</body>
	</html>

Most JavaScript programs contain many JavaScript statements.

The statements are executed, one by one, in the same order as they are written

Semicolons ;

Semicolons separate JavaScript statements.

Add a semicolon at the end of each executable statement:

<!DOCTYPE html>
<html>
  <body>
<h2>JavaScript Statements</h2>
     <p>JavaScript statements are separated by semicolons.</p>
<p id="demo1"></p>

<script>
var a, b, c;
a = 5;
b = 6;
c = a + b;
document.getElementById("demo1").innerHTML = c;
</script>
  </body>
</html>

When separated by semicolons, multiple statements on one line are allowed:

a = 5; b = 6; c = a + b;

On the web, you might see examples without semicolons.
Ending statements with semicolon is not required, but highly recommended.

JavaScript White Space

JavaScript ignores multiple spaces. You can add white space to your script to make it more readable.

The following lines are equivalent:

var person = "Hege";
var person="Hege";

A good practice is to put spaces around operators ( = + – * / ):

var x = y + z;