Intro to JS - Basics, Variables, SCRIPT tag
Basic Rules - example source (excerpt from Pace Calculator Source)
The <script> tag has several attributes. For a complete reference, refer to W3C's HTML 4.01 Specification § 18 Scripts.
Zero, one, or more attributes can be used in the same <SCRIPT> tag. The attributes discussed here are language, type, and src.
The W3C has deprecated this attribute in the standard, but it is still widely used and supported by browsers. In fact, the Netscape link above gives several examples using the language attribute.
example - start <SCRIPT> tag using language attribute
The type attribute is required in the W3C HTML 4.01 standard. However, most browsers tend to ignore this requirement. The value of type has to be a valid MIME type.
example - start <SCRIPT> tag using type attribute
An HTML file can use one or more dot-js files. Each needs its own <SCRIPT> tag.
The value for the src attribute is the path and file name to find the dot-js file. If the file is in the same directory as the HTML page that will use it, then just the file name is needed.
example - contents of foo.js
var x = 'value of x'; // set a variable alert(x); // display variable in a popup box.
example - start and end for <SCRIPT> tag using src attribute
Syntax rules have to be followed, but there are also some naming conventions which are not required, but help keep things consistent. Here are some variable naming conventions. Keep the first letter lower-case. And if the name consists of multiple words, run them together and capitalize each new word.
Some variables may not need to be changed throughout the code. They may only be needed as placeholders. If the value is going to stay constant, you can differentiate these variables by making them all upper-case, and separate different words with underscores. This also a naming convention and not required. The term "constant variable" may be an oxymoron, but they are useful. Constants can replace literal values. Literals are hard coded values. For example, when a variable is set to equal 4, the number 4 is a literal. Using constants will help to make the code easier to follow and understand.
variable naming - example
var a = 1; // a is declared, and assigned the number value 1 a = "test"; // a is then assigned the string value "test" var testWithLongVariableName; // declared using typical naming convention var NUMBER_OF_STRIPES = 13; // naming convention in all upper case for constant
A variable that is declared outside any functions is global. This means it can be referenced anywhere in the current document.
- Declared outside any functions, with or without the var keyword.
- Declared inside a function without using the var keyword, but only once the function is called.
A variable that is declared inside a function is local. This means it can only be referenced within the function it is declared.
- Declared in a function with the var keyword.
If a variable is declared inside a conditional statement, it is still available anywhere following the declaration in the containing function (or globally if the conditional is not in a function). However, it will equal undefined if the condition evaluates to false, unless the variable is later assigned a value.
variable scope - example