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

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 ( _ ).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值