What is the difference between:
Ex 1.
<script type="text/javascript">
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
</script>
Ex 2.
<script type="text/javascript">
{
document.write("<h1>This is a heading</h1>");
document.write("<p>This is a paragraph.</p>");
document.write("<p>This is another paragraph.</p>");
}
</script>
W3C Schools says:
Ex 1. Each statement is executed by
the browser in the sequence they are
written.
Ex 2. The purpose of a
block is to make the sequence of
statements execute together.
Please explain the above statements. How does the browser acts differently upon the above commands?
No difference at all, the purpose of a block is to be used in ifs and fors, like so:
Ex 1
if ()
//command to execute
Ex 2
if ()
{
// many commands to execute
}
In the second sample, the if statement “sees” the block command as a single command, inside the block, the commands still are executed in order.