PART I: AT THE GATES OF ENLIGHTENMENT
1.3 Declaring a variable – an alias name for a memory location
Browsers use computer memory. The more memory a computer has, the faster the browser works. Since working memory is limited, programs need to constantly rearrange data in memory by deleting what expires and repositioning what is still in use.
To temporarily store data in memory we need to label the memory address location with a word that makes sense to humans, so that we can program or manipulate that data. We can’t just tell the browser to store the string of characters “hello world!” in memory by giving it the address of0x7fffffff. Those days are long gone as it would be too complex and unpractical as well as ever changing since data is shifted around from location to location due to automatic memory rearrangements. So to address this problem, we use names as alias for assigned locations and allow the computer to manage the real physical address of the data as it sees fit.
Those memory location aliases are called variables. The reason they are called variables is twofold: the address of the data can be changed without notice, and the data contained in each variable can also be changed at any time. What remains constant is the variable name, which is the only link humans have to this piece of memory real estate.
(Just read along. You will have a chance to try it yourself when we get to the Lab Work section at the end of the chapter. All chapters are purposely short. This is the time to read and understand conceptually. Then you will test yourself during lab. All lab exercises are followed by the correct answers so that you can double check your work).
Think of var as the command used to create a variable (this helps you remember to use var when you introduce a variable.
Another thing that var does is to ground the variable to the location where it is being introduced, sort of like making it a local variable (more about that later).
About the variable name
· In summary: always start your variable names with a lower case letter. Forget about the underscore as the first character because it is usually used in very special situations and therefore should not be considered for normal usage.
Another popular way of writing names is with an underscore separating the ‘human meaning’ of each word in the variable name. The reason for the underscore is because spaces or dashes are not permitted in variable names: phone_bill
About the semicolon ;
Going back to the variable name,
Adding data to variables
Once a variable is declared, a memory location is reserved for that variable and then we can assign the variable to some data.
Isn’t it easier to just remember phoneBill rather than making a written map of reserved memory locations?
To assign data into a variable we use the = operator:
phoneBill = 200;
I have just saved the number 200 in a memory location listed as phoneBill. Notice the semicolon at the end. It terminates my statement.
We could have declared and also included the data in just one-step:
var utilitiesBill = 400;
Changing the value in the variable
What if we want to change the value of our phoneBill from 200 to 150?
To change the value of a variable all we have to do is to reassign it (no declaration needed):
phoneBill = 150;
The 200 gets deleted and the computer memory at location phoneBill now holds the value of 150.
· Remember this: Never use var twice on the same variable name. If you use var as a prefix when you rewrite an existing variable, you wipe out the old variable since you have just re-declared the variable again. This may be something you want to do on purpose, but for the most part, this is not a common practice. I have seen this done many times as a mistake by new programmers.
Adding words to a variable instead of numbers
So far we have been assigning data of type number to our variables. We can actually assign anything to a variable.
Since a variable is just a memory location, we can assign it a string of characters also known as a value of type string, or even other types of data such as arrays, functions, objects... more about those later.
Let’s talk about strings
var myName = "Tony";
After we declare the variable myName, the word myName then becomes a reserved term until it gets wiped out from memory or until the program is over. No quotes for variable names.
· Remember this: Data of type string needs to be wrapped in quotes as opposed to declared variables names which are never written in quotes.
When I say “remember this” it doesn’t mean to memorize it. It only means to focus a few seconds of extra attention to the statement that is about to follow.
Which quotes to use: single or double?
var quoteOfTheDay = '"living is not essential; creating is essential" - Fernando Pessoa';
Assigning variables to other variables
We have seen already how to reassign a variable to a new value:
phoneBill = 600;
Now, phoneBill is no longer 150. It holds the value of 600.
What about assigning a variable to another variable?
It works the same way as assigning a regular value:
phoneBill = quoteOfTheDay;
Note: No quotes are being used because both words are previously declared variables. The real value, which is a string value, is being passed inside of the variable quoteOdTheDay.
· Remember this rule:
Assignments work from right to left. What is on the right gets assigned to the left operand.
The value of phoneBill has now been changed from the numeric 600 to the string "living is not essential; creating is essential" - Fernando Pessoa.
We will see more about primitive and reference types later in the book. Sometimes I throw in terms without an explanation. The idea is to get you used to the term as common language when you read along and without memorizing it. A former introduction will happen at the appropriate time. Occasionally you will see a link to sources such as Wikipedia. The link is there in case you want to explore the topic further, but no external link it essential in order to understand the material we are covering. Wikipedia is my favorite source of general information for two reasons: It gets updated quickly and it does not have annoying popup commercial messages.