Revolution X

Coding Community => Tutorials => Topic started by: Agent Moose on May 19, 2009, 09:32:08 pm



Title: Tutorial #2: Variables
Post by: Agent Moose on May 19, 2009, 09:32:08 pm
What is a Variable?
A variable is a "container" for information you want to store. A variable's value can change during the script. You can refer to a variable by name to see its value or to change its value.

Ok, How do I use them?
There are two ways you can start out a variable.  You can use this;
Code:
<script>
var name = value
</script>

Or, you can use a shorter way:
Code:
<script>
name = value
</script>

It doesn't matter which one you use.

A very inportant thing, you may have a variable named whatever you want, as long as there are no spaces.  So, you can use this:
Code:
<script>
var myVariable = "Hello There"
</script>
or this:
Code:
<script>
var my_variable = "Hello There"
</script>
But you can't use this:
Code:
<script>
var my ariable = "Hello There"
</script>

What exactly would I use them for?
As you read above, variables are "containers".  They can hold HTML, or text, or even other scripts just as Jquery and Javascript codes.

Here is an example of useing a variable for text...
Code:
<script>
name = "Text is being Used";
</script>

Just a little note, the ";", it is good to have that at the end of everyline when ever you are doing javascript, but you don't have to.  It just says that that line is done.

Now, that will not show anywhere because it doesn't have anything to make it show.  To make it you would use this...
Code:
<script>
name = "Text is being Used";
document.write(name);
</script>
As you can see, the document.write() is holding the variable name, "name".  So, the text that would show would be "Text is being Used" since that is the text in the variable.

You may use HTML in the variables.  Just one thing you need to remember, you can't use double quotes, you must use Single Quotes.

Now for somthing that is a bit harder.
Code:
<script>
var my_spans = document.getElementsByTagName("span");
document.write(my_spans.length);
</script>
The variable "my_spans" is getting all the tags with SPAN in them, and then writing how many there are on that page.  If you change span to table, tr, td, a, or any other tag, it would do the same exact thing, but the number may be diffrent.

Now useing the variables in an if...else statement.
Code:
<script>
var x = "2";

if(x == "2"){
  alert("That is the right number.");
}else{
  alert("Not the right number.");
};
</script>
In the above code, the variable is x, and it has the value of 1.  The if statement is reading the variable, and seeing if is it equal 2(The == means "is equal to").  Since x is equal to 2, then you will get an alert saying "That is the right number.".  If the variable was equal to something else, then you will get the alert "Not the right number.".


Title: Re: Tutorial #2: Variables
Post by: Andrew on May 20, 2009, 07:37:21 am
Very nice agent, this helps of understand codes a bit more. great tut :)


Title: Re: Tutorial #2: Variables
Post by: Agent Moose on May 20, 2009, 07:45:46 am
Glad you like it :)  I've got 3 more tutorials that will help you so you can start coding, if you want to :P


Title: Re: Tutorial #2: Variables
Post by: Andrew on May 20, 2009, 12:17:06 pm
Too bad i cant code on a phone.... but i will start soon, probally this weekend.