ÄúµÄλÖãºÑ°ÃÎÍøÊ×Ò³£¾±à³ÌÀÖÔ°£¾JavaScript£¾Javascript Tutorial
Javascript Tutorial

Javascript - Intro
Javascript - Syntax
Javascript - Location
Javascript - External
Javascript - Operators
Javascript - Variables
Javascript - Functions
Javascript - Events
Javascript - Statements
Javascript - If
Javascript - Else If
Javascript - While
Javascript - For Loop
Javascript - Comments
Javascript - Array
Javascript - Alert
Javascript - Confirm
Javascript - Prompt
Javascript - Print
Javascript - Redirect
Javascript - Pop Up
Javascript - Date
Javascript - Form
Javascript - Void 0

Javascript String

Javascript - Strings
Javascript - Length
Javascript - Split
Javascript - Search
Javascript - Replace
Javascript - indexOf
Javascript - Compare

Javascript Advanced

Javascript - getElementById
Javascript - innerHTML

JavaScript Variables

Variables in Javascript 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 important!

Javascript Using Variables

A variable's purpose is to store information so that it can be used later. A variable is a symbolic name that represents some data that you set. To think of a variable name in real world terms, picture that the name is a grocery bag and the data it represents are the groceries. The name wraps up the data so you can move it around a lot easier, but the name is not the data!

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:

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 assign 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 reserved names Found Here . Another good practice is choosing variable names that are descriptive 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).