JavaScript Resources

Syntax

Your computer, of course, can only execute the commands that it is given. As a programmer, this means, if you mistakenly give the computer the wrong command, it will not know. The computer will execute any correctly-phrased command that it is given. Similarly, if you phrase the command in a way that the computer is not expecting, you will witness the digital equivalent of a "hissy fit". (If you haven't already in your JavaScript endeavors.) The words and/or symbols that are used to issue commands have to be positioned, spelled, and spaced correctly.

The syntax for doing this is as follows:

  • JavaScript is case sensitive. In HTML: <BODY BGCOLOR="ffffff">and <body bgcolor="FFFFFF">produce the same result. In JavaScript: window.alert and window.Alert are not the same thing.

  • Hitting the return or enter keys signify the end of a line of instruction.

  • Since these keys end instructions, it is O.K.. to let lines of text wrap over several lines producing one really long statement.

  • Similar to HTML, you can sprinkle white space (space bar, tab, and return key strokes) throughout your script as long as it does not effect the syntax of your statements. So, placing a return character in the middle of "window.alert" wouldn't be such a great idea. However, separating a statement that consists of a window.alert() method from another statement that consist of a window.document.write() method with one or two return characters is completely acceptable. Thus, you can use white space to organize your code so that it is easier to read. You should use the tab character to indent lines of code that will execute together, i.e. statements within a function or conditional statement. This will make editing a debugging a whole lot easier.

  • It is optional to end each instruction on its own line with a semicolon ";". Because other programming languages usually require a semicolon, JavaScript allows you to include one just in case you add it out of force of habit (which is what I do frequently). You can also use the semicolon to separate instructions that are on the same line. This is necessary when giving several instructions to an event handler.

  • Most symbols (e.g. - + = ( ) " " ' ' [ ] { } *. etc.) have special meanings. Avoid using bare symbols in the strings that you give to commands unless you are sure that it is O.K.. When you absolutely need to use a symbol, it is a good idea to precede the symbol with a backslash. This temporarily turns off the special meaning of the symbol.

  • Single and double quotes are the most commonly backslashed special characters. When using a set of quotes within another set of quotes, you will need to precede the internal qutoes with backslash characters. For example, if you wanted to display something in an alert message like the phrase "tech problems":

      window.alert("Report all "tech problems" to the instructor.")

    This would give you an error. The only way to do it is as follows:

      window.alert("Report all \"tech problems\" to the instructor.")

    This is because the quotation marks have a special meaning in JavaScript. Double quotes determine the beginning and end of data that you are passing to a command. This, of course, also applies to single quotes:

      window.alert("Let\'s get ready to rumble!")

  • Parentheses are used in pairs. Anytime that you use an opening parenthesis ( to identify information to the JavaScript interpreter, remember that you must also use the closing parenthesis ) to end this identification.

  • Curly braces are used in pairs. Anytime that you use an opening curly brace { to identify information to the JavaScript interpreter, remember that you must also use the closing curly brace } to end this identification. This is difficult to see and remember when you are working with nested structures, i.e. a conditional statement within a conditional statement within a function.