Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

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

  • Print
  • Feedback
Programming languages are next to useless without variables. These special information-holding areas can store numbers, text strings, objects, and other data types. Once stored, this information can be used later in your program. With a variable, a person's name could be stored and used at some point in the script.

Variables are temporary holders of information. They can hold:

  • Numeric values ("numbers") -- numbers that can be added together. Example: 2+2 results in 4
  • Character strings -- a collection of text, such as "JavaScript" or "My name is Mudd"
  • True/false values -- the Boolean true and false
  • Objects -- JavaScript or user-defined objects (JavaScript variables can hold a few other kinds of data, but these are by far the most common types)


(Note: As with most modern programming languages, JavaScript supports array variables in addition to the basic scalar variables used to hold the above data types. We'll concentrate on single-value variables for this column and devote a separate column to arrays.)

JavaScript variables "belong" to the script document that created them; the variables are lost when the document is unloaded. In addition, the contents of a variable are erased when you assign a new value to them. Though a variable created in one document script is not usually seen by another document script, JavaScript does provide ways to share variables between scripts. You do this by referencing the name of the document along with the name of the variable.

Several JavaScript instructions create and store variables, but the basic way to accomplish this manually is with the equals (=) assignment operator. The basic syntax is:

VariableName = value


The first argument is the name of the variable. Variable names can be very long, but you are restricted in the characters you may use. For more information on valid variable names, see the section on Variable name limits.

The second argument is the contents of the variable. You can put all sorts of stuff into a variable, including a number, a string, a math expression (such as 2+2), and various other things that we'll get to in a bit.

Pascal users may be tempted to construct the variable assignment using :=. Be aware that this syntax is not supported in JavaScript.

Following is a more specific rundown of the four most common contents you can place in JavaScript variables, including examples.

Contents placed in JavaScript variables

Numbers in variables
A number is one or more digits stored in the computer in such a way that JavaScript can perform math calculations with them. JavaScript supports both integers and floating-point values. To place a number in a variable, just provide the variable name, the equals sign (the variable assignment operator), and the value you want to use. For example, the following is what you do to place the number 10 in a variable named MyVar:

MyVar = 10;


Strings in variables
A string is one or more text characters arranged in memory in single file. Strings can contain numbers (digits), letters, punctuation, or a combination of these elements. Math calculations cannot be performed on strings. Strings are assigned to JavaScript variables by being enclosed in a set of single or double quotes:

  • Print
  • Feedback

Resources