Welcome to S-Design
The home of your bespoke, professional, fast-loading, interactive, database-driven web presence.


It's difficult to explain what a variable is... but I suppose the simplest answer would be "it's a temporary storage area" or "it's a place to store data in temporarily". Programming languages like PHP, or JavaScript, would be virtually useless without a way to store certain bits of information, and that's where variables come in.

In Javascript, variables start with a "var" prefix if you want that variable to have global scope (accessible everywhere, even inside functions), or you can simply assign it. So "var hello" is a variable called "hello". To store data in that variable, we would simply say:
var hello = "Hi, my name is Simon";

What we've done there, is to say that the value of the variable hello equals (=) whatever is inside the quote marks. The semicolon (;) at the end signifies the end of the variable and MUST NOT be missed off, otherwise you'll get an error message!

Once you have something stored in a variable, you can use it anywhere in your script:
var hello = "Hi, my name is Simon";
<div id="welcome">
Here we've assigned some text to the variable hello, and then we've inserted it into the div with an id of "welcome"

The purpose of this example is to try and demonstrate that a JavaScript variable can be accessed, referenced, and used as many times as you like... for the life of that script. Once your script ends, and the html page is unload (navigated away from)... any data stored in a variable is lost, unless the value of that variable is re-assigned on the start of each script on the next page. There is no easy way to 'transfer' the value of a JavaScript variable from one page to another. JavaScript has no memory!!!

JavaScript variables that contain a string of text (letters & numbers) are known as String Variables (see JavaScript Strings for more). There are other types of variables that look the same but contain more complex information. See the section on JavaScript Arrays for more information.