您的位置:首页 > Web前端 > JavaScript

JavaScript_A Beginner's Guide - Understanding variables - 09/21/2012

2012-09-23 19:28 369 查看
A variable represents or holds a value. The actual value of a variable can be changed at any time. To understand what a variable is, consider a basic statement that you may recall from algebra class:

x=2

The letter x is used as the name of the variable. It is assigned a value of 2. To change the value, you simply give x a new assignment:

x=4

The name of the variable stays the same, but now it represents a different value.Variables in JavaScript are much like those used in mathematics. You give a variable a

name, and then assign it values based on your needs. If the value of the variable changes, it will change something that happens within the script.

Oftentimes, a variable will hold a place in memory for a value that is unknown at the time the script is written. A variable value might change based on something entered by the viewer or may be changed by you later in the script code.For instance, you might
have a function that takes in certain values based on user input. Since the value of user input is unknown at the time the script is written, a variable can be used to hold the value that will be input by the user.

Variables speed up script writing because their values can change. When you assign a value to a variable at the beginning of a script, the rest of the script can simply use the variable in its place. If you decide to change the value later, you need to change
the code in only one place— where you assigned a value to the variable—rather than in numerous places.

Since variables represent something, and you can give them meaningful names, they are often easier to recognize when you read over (and debug) your scripts.

TotalPrice=CandyPrice+OilPrice;

Now, rather than trying to remember the meaning of the numbers, you can see that the script is adding the price of some candy to the price of some oil. This is also useful in debugging, because the meaningful variable names make it easier to spot errors.

To declare text as a variable, you use the var keyword, which tells the browser that the text to follow will be the name of a new variable:

var variablename;


For example, to name your variable coolcar, the declaration looks like this:

var coolcar;

If you want to declare a variable and assign a value to it on the same line, use this format: var variablename=variablevalue;

JavaScript will often declare the variable the first time it is used even if it is previously undeclared. An example is shown here:

paycheck=1200;

This works since the variable is being assigned a value

All of these shortcuts may seem handy, but it is best to go ahead and define each variable before using it, use the var keyword, and include the semicolon.

The correct declarations and assignments will avoid problems, and your code will be easier to read and understand.

JavaScript variables are case sensitive—paycheck, PAYCHECK, Paycheck, and PaYcHeCk are four different variables.

An important rule to remember is that a variable name must begin with a letter or an underscore character ( _ ).
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: