March 28, 2024, 05:43:30 am
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Welcome to Revolution X, where Coding meets Graphics.
 
  Home Help Search Arcade Affiliates Staff List Calendar Members Login Register  

Tutorial #2: Variables

Pages: [1]   Go Down
  Print  
Author Topic: Tutorial #2: Variables  (Read 662 times)
0 Members and 1 Guest are viewing this topic.
Agent Moose
Administrator
Adminitrator
Offline Offline

Gender: Male
Posts: 1,470



View Profile WWW
Badges: (View All)
Tenth year Anniversary Nineth year Anniversary Search
« 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.".
Report Spam   Logged


Share on Facebook Share on Twitter

Andrew
Exclusive
Exclusive
Offline Offline

Gender: Male
Posts: 539



View Profile WWW
Badges: (View All)
Combination Topic Starter Poll Voter
« Reply #1 on: May 20, 2009, 07:37:21 am »

Very nice agent, this helps of understand codes a bit more. great tut Smiley
Report Spam   Logged

Agent Moose
Administrator
Adminitrator
Offline Offline

Gender: Male
Posts: 1,470



View Profile WWW
Badges: (View All)
Tenth year Anniversary Nineth year Anniversary Search
« Reply #2 on: May 20, 2009, 07:45:46 am »

Glad you like it Smiley  I've got 3 more tutorials that will help you so you can start coding, if you want to Tongue
Report Spam   Logged


Andrew
Exclusive
Exclusive
Offline Offline

Gender: Male
Posts: 539



View Profile WWW
Badges: (View All)
Combination Topic Starter Poll Voter
« Reply #3 on: May 20, 2009, 12:17:06 pm »

Too bad i cant code on a phone.... but i will start soon, probally this weekend.
Report Spam   Logged

Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum

Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy