Intro. to Variables in JavaScript:

A variable is an invisible container to hold information.
It is named so that you can refer to it clearly in a script.

Variable Names:

JavaScript variables last only for the page they are on. If you go to another page (or even reload the page) the variable is erased from memory. To remember information from page to page using JavaScript, you would need to use JavaScript cookies.

Declaring a Variable:

To declare a variable, use the term var and then the variable name, and then set the variable to its initial value (what you want the variable to start off holding).

This line of script declares a variable, and sets it to hold an empty string:

var pickYerPoison = "";

This line of script declares a variable, and sets it to hold a number:

var luckyNum = 30;

This line of script declares a variable, and sets it to hold what the script gets back from the prompt:

var users_day = prompt("How is your day going?");

users_day is the variable, and it will hold whatever the user types in as a result of the prompt() command.


You only need to declare the variable the first time it is used. Other lines containing the variable only need to refer to it by name.


What Variables Can Hold:

Variables can be set to hold things including: (this is not an exhaustive list)

You Can Use Variables For: (this is not an exhaustive list)


JavaScript Variable Scope:

Please be aware that the rules for variable scope vary in different scripting or programming languages. JavaScript variable scope is particular to JavaScript, though some aspects may be similar to variable scope in other languages.

A Local variable is a JS variable declared and used inside the same function

Local variables in JS remember information inside the function they have been declared in. After the function finishes executing its current run, the variable is forgotten.

If different functions all use local variables with the same name, the variables don't generally conflict because they are local, and only active while each function is running. Local variables in a function only remember their information as long as it takes for that function to execute completely, and then they are gone, which sometimes allows limited reuse of local variable names between functions.

A Global variable is a JS variable declared inside script tags (not declared inside a function), and then used in script tags and possibly in a function (or in several functions)

Global variables in JS remember information inside AND outside functions, and from function to function.

This means that the information in a global variable can be accessed and used in multiple functions, and/or outside of functions in script tags!

If different functions all use global variables with the same name, the variables are really all the same variable, sharing information between functions.

JS Cookies are not technically variables, but are a memory technology as variables are.

JavaScript cookies remember information from page to page, and for potentially long periods of time between visits to pages.