- 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.
- Creo Parametric 8.0中文版基礎入門一本通
- 中文版AutoCAD 2015實用教程
- Sakai CLE Courseware Management
- Adobe Photoshop 網頁設計與制作標準實訓教程(CS5修訂版)
- 中文版Premiere影視編輯課堂實錄
- 印象筆記留給你的空間2.0:個人知識管理實踐指南
- 輕松玩轉3D One AI
- NHibernate 3.0 Cookbook
- Photoshop CS6實戰從入門到精通(超值版)
- 會聲會影視頻編輯實戰秘技250招
- AutoCAD 2016入門與提高(超值版)
- Photoshop CC2017圖像處理實例教程
- UG NX12中文版實用教程
- Adobe創意大學Premiere Pro影視剪輯師標準實訓教材(CS6修訂版)
- AI寫實人物繪畫關鍵詞圖鑒(Stable Diffusion版)