Richard's JavaScript Tutorials

Update 07/18/14

Home
Animation
precachedImages
digitalClock
Object Tag (Audio)
Cookies.htm
Math_Object
Using CSS
The Style Object
Client_Info
customObject
functions
UsingArrays
comments
concatenation
documentwrite
ifelse
Loops
forms
Strings
variables

 

Declaring and using variables

In JavaScript declaring variables is similar to other declaration techniques in other common programming languages

var nameOfVariable

Where "nameOfVariable" is the name of the new variable being declared. Once the variable has been declared you can use it later in the script. Here is a simple example of the declaration and use of variables.

<script type="text/javascript">
var favoriteColor ="blue";
if(favoriteColor="blue")
document.write("My favorite color is blue");
else
document.write("My favorite color is not blue");
</script>

Click here to execute my favorite color script.

In this example the favoriteColor variable is given a value of "blue", making the if part of the statement true and executing the document.write() method under the if part of the statement.

Try modifying my code and assign a different value to the favoriteColor variable The if part of the statement would then be false and the code under the else part of the statement would execute. In this case it is a document.write method() and you should see

My favorite color is not blue

after changing the value of the favoriteColor value.

 

Home | Animation | precachedImages | digitalClock | Object Tag (Audio) | Cookies.htm | Math_Object | Using CSS | The Style Object | Client_Info | customObject | functions | UsingArrays | comments | concatenation | documentwrite | ifelse | Loops | forms | Strings | variables

This site was last updated 07/08/14