- Object/Oriented JavaScript
- Stoyan Stefanov
- 427字
- 2021-08-13 19:25:51
Variables
Variables are used to store data. When writing programs, it is convenient to use variables instead of the actual data, as it's much easier to write pi
instead of 3.141592653589793 especially when it happens several times inside your program. The data stored in a variable can be changed after it was initially assigned, hence the name "variable". Variables are also useful for storing data that is unknown to the programmer when the code is written, such as the result of later operations.
There are two steps required in order to use a variable. You need to:
- Declare the variable
- Initialize it, that is, give it a value
In order to declare a variable, you use the var
statement, like this:
var a; var thisIsAVariable; var _and_this_too; var mix12three;
For the names of the variables, you can use any combination of letters, numbers, and the underscore character. However, you can't start with a number, which means that this is invalid:
var 2three4five;
To initialize a variable means to give it a value for the first (initial) time. You have two ways to do so:
- Declare the variable first, then initialize it, or
- Declare and initialize with a single statement
An example of the latter is:
var a = 1;
Now the variable named a
contains the value 1
.
You can declare (and optionally initialize) several variables with a single var
statement; just separate the declarations with a comma:
var v1, v2, v3 = 'hello', v4 = 4, v5;
Variables are Case Sensitive
Variable names are case-sensitive. You can verify this statement using the Firebug console. Try typing this, pressing Enter after each line:
var case_matters = 'lower'; var CASE_MATTERS = 'upper'; case_matters CASE_MATTERS
To save keystrokes, when you enter the third line, you can only type ca and press the Tab key. The console will auto-complete the variable name to case_matters
. Similarly, for the last line—type CA and press Tab. The end result is shown on the following figure.

Throughout the rest of this book, only the code for the examples will be given, instead of a screenshot:
>>> var case_matters = 'lower'; >>> var CASE_MATTERS = 'upper'; >>> case_matters
"lower"
>>> CASE_MATTERS
"upper"
The three consecutive greater-than signs (>>>) show the code that you type, the rest is the result, as printed in the console. Again, remember that when you see such code examples, you're strongly encouraged to type in the code yourself and experiment tweaking it a little here and there, so that you get a better feeling of how it works exactly.
- Expert Cube Development with Microsoft SQL Server 2008 Analysis Services
- 中文版SolidWorks 2015技術大全
- Flash CS6標準教程(全視頻微課版)
- Oracle VM Manager 2.1.2
- 改變思維:菜鳥也能做出震撼PPT(全彩版)
- 跟儲君老師學Excel極簡思維
- MATLAB 2015從入門到精通
- After Effects CC 2018影視特效與合成案例教程
- Android從入門到精通
- Designing and Implementing Linux Firewalls and QoS using netfilter, iproute2, NAT and l7/filter
- Photoshop CC2017圖像處理實例教程
- Microsoft SQL Azure Enterprise Application Development
- Premiere Pro基礎與實戰教程
- SketchUP草圖繪制從新手到高手
- Plone 3 Theming