Recent articles:
Popular archives:
Java: A platform for platforms
Sun's reorg may seem promising to shareholders but it's also a scramble for position. The question now is whether Sun can,
or wants to, maintain its hold on Java technology. Especially with enterprise leaders like SpringSource and RedHat investing
heavily in Java's future as a platform for platforms
Also see:
Discuss: Tim Bray on 'What Sun Should Do'
Page 3 of 4
JavaScript supports a var statement that can be used to explicitly define a variable. The syntax is merely the statement var, a space, and the same variable assignment expression detailed above. For instance:
var MyVar = "This is a variable";
You can also use the var statement with the variable name to declare the variable but not define a value for it:
var MyVar;
In this case, you've defined MyVar in memory but have yet to assign a value to it. This technique is often used when setting up global variables -- variables that can be freely shared anywhere in your script. For more information about global variables, see the section "Understanding the scope of variables", below.
JavaScript imposes a limit of 254 characters for each string variable assignment in your program. If you go over the 254-character limit, JavaScript responds with a "Unterminated string literal" error message. (Note: This is fundamentally a limit of JavaScript in Netscape 2.0x; it's a good idea to observe it since not all users have adopted Netscape 3.0.)
You can create longer strings by "piecing" them together -- as long as each piece is 254 characters or less. After assigning a string to each variable, you combine them using the + character. This is called "concatenation." The following example shows how concatenation works:
MyVar = "This is the start " + of how you " + " can build strings";
Each individual string segment -- defined by text within the quotes -- can be up to 254 characters. To make a string longer than 254 characters, merely add more segments. Another approach is to build strings using the += assignment operator, like this:
MyVar = "This is the start " MyVar += "of how you " MyVar + = can build strings "
You can continue to concatenate strings this way as long as your computer has the memory for it. But, while JavaScript can hold strings larger than that possible in many other programming languages (like Basic's typical 64K), doing so can severely degrade the performance of the system. Obviously, you won't create a lot of huge string variables. It's just nice to know that, if needed, a JavaScript variable can accommodate so much text.
The "scope of a variable" has nothing to do with optics or mouthwash, but rather the extent to which a variable is visible to other parts of a JavaScript program. Unless you provide explicit instructions to tell JavaScript otherwise, the scope of its variables is managed as follows:
Local variables are treated as if they don't exist outside the function where they are defined. That way, you can use the same variable name inside a function, and that variable won't interfere with the same-named variable elsewhere in the script.