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'

Featured Whitepapers
Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Take advantage of user-defined variables in JavaScript

Learn how to master variables, the special information-holding areas that are crucial to a programming language

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone

Page 3 of 4

Using the var statement to assign a variable

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.

String length limitations

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.

Understanding the "scope" of variables

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:

  • Variables defined outside a function are available to any function within the script, as long as all the variables are definined in the script of the same HTML document. These are referred to as global variables.

  • Variables defined inside a function are also global, assuming the var statement is not used when first declaring that variable. That is, MyVar = "hello."

  • Variables defined inside a function with the var statement are "local" to that function only. These are referred to as local variables.

  • Global variables remain in memory even after a script has stopped execution. The variable remains in memory until the document is unloaded.


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.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comment
Login
Forgot your account info?
Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources