Hello, fellow STC members. In my last entry, we discussed the purpose and characteristics of JavaScript. Since we now know their purpose, the larger question remains: How do we use them? Well, let’s find out. John Pollock, the author of JavaScript: A Beginner’s Guide, points out that variables can be used to substitute long and repetitive code in a script. For the purpose of this example, I’ll use a variable to substitute a long warning label for a fictitious drug:
Side-effects include but are not limited to grand mal seizures, paralysis, homicidal thoughts,
nosebleeds, brain hemorrhages, delusions of omnipotence, and mild nausea.
Now let’s pretend that the twisted pharmaceutical corporation who makes the medication is required by law to post this warning throughout multiple pages of its website. What a hassle!
For a moment, let’s forget that copy and paste exists, then think of another way to write this warning throughout the website. What do we do? You guessed it. We turn that long warning label into a text-string variable.
var badmedicine=”Side-effects include but are not limited to grand mal seizures, paralysis, homicidal thoughts, nosebleeds, brain hemorrhages, delusions of omnipotence, and mild nausea.”
Whenever we need to write the warning label in an HTML document we simply replace the phrase with the text-string variable. Now let’s take this concept one step further. Let’s add some cheerful marketing rhetoric to see if we can distract consumers away from that dreadful warning label.
var damagecontrol=”Our product will let you live a very interesting life, and give you the confidence of a supreme being.”;
Now, let’s add those two variables together:
<script type=”text/javascript”>
var badmedicine=”Side-effects include but are not limited to grand mal seizures, paralysis, homicidal thoughts, nosebleeds, brain hemorrhages, delusions of omnipotence, and mild nausea.”;
var damagecontrol=”Our product will let you live a very interesting life, and give you the confidence of a supreme being.”;
document.write(badmedicine+damagecontrol);
</script>
On an HTML file, those two variable will appear as two sentences:
Side-effects include but are not limited to grand mal seizures, paralysis, homicidal thoughts, nosebleeds, brain hemorrhages, delusions of omnipotence, and mild nausea. Our product will let you live a very interesting life, and give you the confidence of a supreme being.
Well, that wraps it up for Chapter Three. Let’s take a peek at Chapter Four, shall we? Chapter Four deals with functions. According to Pollock, a function is a script within a script. It can do a variety of things such as write lines of script or determine numerical values and give that data to the main script. We’ll learn more about functions next time as I go through Chapter Four. Thanks for reading!
{ 0 comments… add one now }