JS Code Statements

JavaScript code (or just JS code) is a sequence of JavaScript statements. Each statement is executed by the browser in the sequence they are written. Actually a JS code statement is a command to the browser. The purpose of the command is to tell the browser what to do. This example will write a header and two paragraphs to a web page:

<html>
<body>
<script type="text/javascript">
document.write("<h2>This is a header</h2>");
document.write("<p>This is a paragraph</p>");
document.write("<p>This is another paragraph</p>");
</script>
</body>
</html>

It is to be noted that unlike HTML, JS is case sensitive – therefore watch our capitalization closely when we write JS statements, create or call variables, objects and functions. Another important thing is that the use of semicolon is optional (according to the JS standard), and the browser is supposed to interpret the end of the line as the end of the statement. Because of this we will often see examples without the semicolon at the end.

Therefore the statement document.write(“Hello From JavaScript”);

in JS is same as document.write(“Hello From JavaScript”)

Note: Using semicolons makes it possible to write multiple statements on one line.

 

JavaScript Blocks

JS statements can be grouped together in blocks. Blocks start with a left curly bracket {, and ends with a right curly bracket }. The purpose of a block is to make the sequence of statements execute together. This example will write a header and two paragraphs to a web page:

<html>
<body>
<script type="text/javascript">
{
document.write("<h2>This is a header</h2>");
document.write("<p>This is a paragraph</p>");
document.write("<p>This is another paragraph</p>");
}
</script>
</body>
</html>

The example above is not very useful. It just demonstrates the use of a block. Normally a block is used to group statements together in a function or in a condition (where a group of statements should be executed if a condition is met).