![]() ![]() ![]() ![]() |
![]() |
Chapter Four All the Logic You Need to Know |
Yesterday I Couldn't Spell It and Today I Are a Web Guru I really hate this. But ya gotta know how JavaScript logic is specified. Otherwise your scripts can't do much. The Symbols: The following symbols are used in JavaScript for comparisons:
The following symbols are used in JavaScript for actions:
The following symbols are used in special ways JavaScript for actions:
If Something, Then Something To use comparisons of one kind or another in your script, the if-then logic determiner is your tool. If, for example, the hours in a military time string are greater than 12, you will want your script to subtract 12 from that value to get the time of day in civilian time. (And add a "p.m." as well) How do you do this in JavaScript? The if comparison takes this form: How does it work in practice? The foregoing code can be shortened significantly using the ?a:b format of the if-then conditional. Pay particular attention to the double equal sign when making logic statements. If you use the following: you will always get an error. The correct form must use the double equal sign to test for equivalence: "If" statements are evaluated by the JavaScript when it executes. If the stuff inside the parentheses evaluates to "true", then the stuff that follows inside the curly brackets is executed. If the stuff inside the parentheses ain't true, it simply skips the stuff inside the curly brackets. If Something, Then Do Something, Otherwise Do Something Else This logic is called "If-Then-Else". And all you do is add one more component to the "If-Then" statement. While Something is True, Keep On Doing Something The "while" logic allows you to establish a "loop" (in a loop an action is repeated over and over for a number of times) for (x=y;limit;modifier) Loop The "for" logic allows you to establish a "loop" that executes a specific number of times. It has a very rigid and inflexible format. If you deviate from the format, you will get an error. Within the parentheses are three separate components, separated by semicolons. The first component within the parentheses is the loop initiator. It sets a loop variable x equal to a value y. This will be a numeric variable. The second component is the loop limiter. It tells JavaScript when to stop looping. The third and final component is the variable modifier. Typically it will be a statement that increments or decrements the loop variable. In the example above, the stuff inside the curly brackets would be executed ten times. Why? We begin by setting "i" equal to zero. Then, until "i" is greater than 9, we will process the stuff inside the curly brackets. The final component increments "i" by one each time the loop is executed. Special Note: Exiting from Loops Prematurely Sometimes you will construct a loop that includes a test to exit or branch prematurely from that loop. It is advisable to always complete the specified terminal condition in the loop limiter. In the example above, you can terminate the loop properly simply by setting "i" equal to 10. You may hear about some so-called "memory holes" in browsers. At least some of these are caused by improper loop termination. A specific memory register is reserved for the loop you create. Until the loop is properly terminated, that register can not be freed up. In iterative code, if you hit that loop again, a new register is reserved, etc. You can run anyone out of memory, given enough iterations. In particular, many of the "scroller" JavaScripts endemic on the Internet today use exactly that kind of code to continuously require more and more memory for each iteration of the "scroll". You don't want your visitors to experience the awful, resounding "Oh, No!" of a browser crash. Order of Analysis - Use of Parentheses JavaScript reads your code from left to right (just like you do) This can cause problems with arithmetic sometimes, because JavaScript is very literal and inflexible. For example: So, make sure you use parentheses to group your arithmetical expressions to yield the result you are really looking for.
One further caution: adding strings concatenates them. Adding strings and numerics will yield a concatenated string. |