JavaScript
Statements
JavaScript statements are "commands"
to the browser. Statements informs to the browser what to do.
·
Semicolon separates JavaScript statements. Every statement ends
with a semicolon.
·
Using semicolon we can write many statements in one line.
·
In javascripts semicolon is optional
JavaScript
Programs(code)
·
JavaScript code (or just JavaScript) is a sequence(collection)
of JavaScript statements.
·
Each statement is executed by the browser in the sequence they
are written.
This example will manipulate two HTML
elements:
Example
<!DOCTYPE html>
<html>
<body>
<h1>My Web Page</h1>
<p id="demo">A Paragraph.</p>
<div id="myDIV">A DIV.</div>
<script>
document.getElementById("demo").innerHTML="Hello
World";
document.getElementById("myDIV").innerHTML="JavaScript
Statements";
</script>
</body>
</html>
JavaScript
Code Blocks
The purpose of a block is to make the sequence
of statements execute together.
A good example of statements grouped together
in blocks, are JavaScript functions.
This example will run a function that will
manipulate two HTML elements:
Example
<!JavaScript
code blocks-->
<html>
<body>
<h1>My
Web Page</h1>
<p
id="myPar">I am a paragraph.</p>
<div
id="myDiv">I am a div.</div>
<p>
<button
type="button" onclick="myFunction()">Try
it</button>
</p>
<script>
function
myFunction()
{
document.getElementById("myPar").innerHTML="Good
Morning";
document.getElementById("myDiv").innerHTML="How
are you?";
}
</script>
<p>When
you click on "Try it", the two elements will change.</p>
</body>
</html>
No comments:
Post a Comment