Hello, everyone. It’s Nick here. I have just gone over Chapter Six of John Pollock’s JavaScript: A Beginner’s Guide and I have come to the conclusion that I am biting off way more than I can chew. If I am going to get through this book and blog my experiences, then I will have to rethink what goes into the blogs and what stays out of them. Frankly, I’ve been giving too many examples of code and not enough content. I am going to change that starting with this blog. After all, these blogs are supposed to express my experiences with JavaScript (as opposed to regurgitating the contents of JavaScript: A Beginner’s Guide). I think I’ll leave most of the code examples to John Pollock. After all, his book has an index while my blogs don’t. Hah!
Now let’s move on to my experiences with Chapter Six of JavaScript: A Beginner’s Guide. Chapter Six deals primarily with conditional statements and loops. Conditional statements help the browser choose whether or not to execute a certain function, depending on the data that it receives from the JavaScript coder or an external user (someone who visits the coder’s website).To give you a visual idea of what a conditional statement is, imagine a cause and effect scenario. First I create a variable for the sky and then I assign it a value of blue. By stating that the sky, is blue, I trigger a popup to appear on my web browser which says “Correct!” On the other hand, I can state that the sky is a different color such as brown. Then I will receive a popup message that says “You must be color-blind. Don’t worry, nobody’s perfect.” Of course, if you live in Los Angeles, brown-colored are quite common. Yuck!
var sky === “blue”;
if (sky === blue) {
window.alert(“Correct!”);
}
else if (sky === “brown”) {
window.alert(“Ha! How’s the air out there in Los Angeles? Are the birds falling out of trees yet?”);
}
else {
window.alert(“You must be color-blind. Don’t worry, nobody’s perfect.”);
Of course, conditional statements can (and often do) become frustratingly complex. For starters, conditional statements can be nested inside other conditional statements. Nesting should sound familiar to those who have experience with HTML. Any time you surround an existing tag with another tag, you are nesting HTML content. As an example I might insert bold tags <b> around a specific section of text in a paragraph <p>.
<p><b> This text is BOLD!</b><p>
In most cases, nesting HTML tags is simple and straightforward. Nesting JavaScript code is NOT! Above, you saw my conditional statement example. Now try to picture nesting a conditional statement into one of the “if” statements. Then picture yourself nesting another conditional statement into that. Brackets within brackets within brackets!
The next time you see one of your coworkers being wheeled out of his/her office brain-dead and drooling, keep in mind that he/she might have nested too many conditional statements into a JavaScript file. Now that we know a bit about conditional statements, it’s time to move on to loops which we will cover next time. Until then, avoid JavaScript nesting. It might break your brain!
{ 0 comments… add one now }