- 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.
- WordPress 2.7 Cookbook
- 設(shè)計(jì)模式之禪(第2版)
- Excel 2010 商務(wù)數(shù)據(jù)分析與處理(第2版)
- Photoshop+Adobe Camera Raw+Lightroom(攝影后期照片潤飾實(shí)戰(zhàn))
- Adobe創(chuàng)意大學(xué)After Effects產(chǎn)品專家認(rèn)證標(biāo)準(zhǔn)教材(CS6修訂版)
- ANSYS 15.0有限元分析自學(xué)手冊
- Google App Engine Java and GWT Application Development
- 皮膚鏡圖像分析與識(shí)別
- Python Testing: Beginner's Guide
- OpenCart 1.4 Template Design Cookbook
- JBoss Tools 3 Developers Guide
- 中文版Photoshop CS6應(yīng)用技法教程
- Photoshop CC入門與提高
- 零基礎(chǔ)攝影后期修圖 Photoshop照片處理輕松入門
- AI設(shè)計(jì)時(shí)代:Midjourney實(shí)戰(zhàn)應(yīng)用手冊