Understanding Variables

Understanding variables is fundamental to programming in any language. A variable is simply a named portion of memory, used to hold information, and is roughly analogous to a folder that can hold a single item. For example, if you were writing a program to add two numbers together and display the result you would need somewhere to store the two numbers that the user supplied. In English, the process would be something like this: Ask the user for the first number. Store the first number in a variable called 'x'. Ask the user for the second number. Store the second number in a variable called 'y'. Add the two values together and store the sum in a variable called 'result'. Display the phrase, 'x + y = result'.

However, variables hold more than just numeric values. Any information that needs to be tracked throughout the lifetime of a program is stored in a variable. JavaScript has a number of different types of variables including numbers, strings, Boolean values, and a special value known as 'null'.

Numbers include two different types of values - integers and floating point values. An integer is a whole number (no decimal values) that is either positive or negative, and is relatively small. The maximum size of an integer depends on the computer that the program is running on. As a general reference, on a 16-bit machine, it's likely to be around plus or minus 32,000. Floating point values, or floats, usually include a decimal point. Floats can be either positive or negative, and generally hold much larger values than integers. However, floating point numbers use more of the host computer's memory to store.

A string is what might be thought of as text. The phrase 'Hello World!' is a string. A string can include letters, punctuation and even numbers. The number three in the string '3 blind mice' is a character, and not the value 'three', as it would be in the expression 3 + 1 = 4.

A Boolean variable can only hold one of two different values: true or false. Boolean values get their name from Arthur Boole, who created a type of algebra that was instrumental to the evolution of computers. As you might have guessed, the Boolean algebra revolves around using only two values (1/0, True/False).

Finally, null is a placeholder value that is used to fill a variable when no value is present. An example might be where a program has to search for a value in a list. If the value is found, it places it in the variable. If it is not found, it might place null in the variable. The program can then later determine whether the value was in the list by checking for a null value. The prompt method, which you will learn about later, is used to input information from the user, and if the user presses the cancel button, the method returns null.

JavaScript is what's known as a loosely typed language, which means that a single variable can take on different value types. If you had a variable named 'temp' you could store either a string or numeric value, based upon the needs of the program. Most programming languages require that you declare what type of information you plan to store in a variable before you use it. JavaScript is a little bit less stringent than other languages.