JS References

JS Objects
JS HTML DOM

Suscriber with us



Receive HTML?

Syndicate

JS Variables PDF Print E-mail

Variables are "containers" for storing information:

x=5;  length=66.10;

Do You Remember Algebra From School?

Hopefully you remember algebra like this from school: x=5, y=6, z=x+y.

Do you remember that a letter (like x) could be used to hold a value (like 5), and that you could use the information above calculate the value of z to be 11?

Sure you do!

These letters are called variables, and variables can be used to hold values (x=5) or expressions (z=x+y).

Variables in Jscript behave the same as variables in most popular programming languages (C, C++, etc) except that you don't have to declare variables before you use them. If you don't know what declaring is, don't worry about it, it isn't significant!

JavaScript Variables

  • As with algebra, JavaScript variables are used to hold values or expressions.
  • A variable can have a short name, like x, or a more describing name like length.  
  • A JavaScript variable can also hold a text value like in carname="Volvo".

Rules for JavaScript variable names:

  1. Variable names are case sensitive (y and Y are two different variables)
  2. Variable names must begin with a letter or the underscore character

NOTE:  Because JavaScript is case-sensitive, variable names are case-sensitive.

A Variable Example

When using a variable for the first time it is not necessary to use "var" before the variable name, but it is a good programming practice to make it crystal clear when a variable is being used for the first time in the program. Here we are showing how the same variable can take on different values throughout a script.

HTML & Javascript Code:

<body>
<script type="text/javascript">
<!--
var linebreak = "<br />"
var my_var = "Hello World!"

document.write(my_var)
document.write(linebreak)

my_var = "I am learning javascript!"
document.write(my_var)
document.write(linebreak)

my_var = "Script is Finishing up..."
document.write(my_var)
//-->
</script>
</body>

Display:

Hello World!
I am learning javascript!
Script is Finishing up...

We made two variables in this example. One to hold the HTML for a line break and the other was a dynamic variable that had a total of three different values throughout the script.

To allocate a value to a variable you use the equal sign (=) with the variable on the left and the value to be assigned on the right. If you swap the order then your script will not work correctly! In english the Javascript "myVar = 'Hello World!'" would be: myVar equals 'Hello World!'.

The first time we used a variable we placed var in front to signify its first use. In subsequent assignments of the same variable we did not need the var.

Javascript Variable Naming Conventions

When choosing a variable name you must first be sure that you do not use any of the Javascript kept names Found Here . Another good practice is choosing variable names that are expressive of what the variable holds. If you have a variable that holds the size of a shoe, then name it "shoe_size" to make your Javascript more readable.

Finally, Javascript variable names may not start with a numeral (0-9). These variable names would be illegal: 7lucky, 99bottlesofbeer, and 3zacharm.

A good rule of thumb is to have your variable names start with a lowercase letter (a-z) and use underscores to separate a name with multiple words (i.e. my_var, strong_man, happy_coder, etc).





Digg!Del.icio.us!Google!Live!Facebook!Slashdot!Technorati!StumbleUpon!Newsvine!Yahoo!Free social bookmarking plugins and extensions for Joomla! websites!
Last Updated ( Monday, 17 March 2008 )
 
< Prev   Next >

Polls

Which is the best Scripting language?