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 are used to direct the execution of a script or program. Values, expressions, operators, comments, and keywords make up the statements. Any JavaScript code you create should ideally be composed of discrete statements.
EXAMPLE
<!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>
let x, y, z; // Statement 1
x = 25; // Statement 2
y = 25; // Statement 3
z = x * y; // Statement 4
document.getElementById("demo").innerHTML =
"The value of z is " + z + ".";
</script>
</body>
</html>
Another one
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>In HTML, JavaScript statements are executed by the browser.</p>
<p id="demo"></p>
</body>
</html>
JAVASCRIPT
document.getElementById("demo").innerHTML = "Hello, Gersan Batiller Rufo.";
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>JavaScript statements are separated by semicolons.</p>
<p id="demo1"></p>
</body>
</html>
JAVASCRIPT
let a, b, c;
a = 37;
b = 13;
c = a / b;
document.getElementById("demo1").innerHTML = c;
When separated by semicolons, multiple statements on one line are allowed:
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>Multiple statements on one line are allowed.</p>
<p id="G1"></p>
<p id="G2"></p>
</body>
</html>
JAVASCRIPT
let a, b, c;
a = 18; b = 12; c = a + b;
document.getElementById("G1").innerHTML = c;
let l, m, n, o, p, q;
l = 230; m = 30; n = 60; o = 35; p = 3; q = l + m - n * o / p;
document.getElementById("G2").innerHTML = q;
Code lines larger than 80 characters are frequently avoided by programmers for better readability.
If a JavaScript statement cannot fit on a single line, it is better to break it after an operator:
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>
The best place to break a code line is after an operator or a comma.
</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML =
"Howdy! It's me... Partner!";
</script>
</body>
</html>
JavaScript statements can be grouped together in code blocks, inside curly brackets {...}.
The purpose of code blocks is to define statements to be executed together.
One place you will find statements grouped together in blocks, is in JavaScript functions:
EXAMPLE
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Statements</h2>
<p>JavaScript code blocks are written between { and }</p>
<button type="button" onclick="myFunction()">Click Me!</button>
<p id="demo1"></p>
<p id="demo2"></p>
<p>Click on below</p>
<button type="button" onclick="myQuestion()">See the question?</button>
<p id="My1"></p>
<p id="My2"></p>
<p id="My3"></p>
</body>
</html>
JAVASCRIPT
function myFunction() {
document.getElementById("demo1").innerHTML = "Howdy!";
document.getElementById("demo2").innerHTML = "It's me again partner, I have a three question for ya.";
}
function myQuestion() {
document.getElementById("My1").innerHTML = "1. How JavaScript Programs works?";
document.getElementById("My2").innerHTML = "2. How JavaScript Statements works?";
document.getElementById("My3").innerHTML = "3. How Window.alert() works?";
}