Java Reference
In-Depth Information
Boolean Data
The use of yes or no, positive or negative, and true or false is commonplace in the physical world. The
idea of true and false is also fundamental to digital computers; they don't understand maybes, only true
and false. In fact, the concept of “yes or no” is so useful it has its own data type in JavaScript: the Boolean
data type. The Boolean type has two possible values: true for yes and false for no.
The purpose of Boolean data in JavaScript is just the same as in the world outside programming: They
enable you to answer questions and make decisions based on the answer. For example, if you are asked,
“Is this topic about JavaScript?” you would hopefully answer, “Yes it is,” or you might also say, “That's
true.” Similarly you might say, “If it's false that the subject of the topic is JavaScript, then put it down.”
Here you have a Boolean logic statement (named after its inventor George Boole), which asks a ques-
tion and then does something based on whether the answer is true or false. In JavaScript, you can use
the same sort of Boolean logic to give our programs decision-making abilities. You'll be taking a more
detailed look at Boolean logic in the next chapter.
Variables — Storing Data in Memory
Data can be stored either permanently or temporarily.
You will want to keep important data, such as the details of a person's bank account, in a permanent
store. For example, when Ms. Bloggs takes ten dollars or pounds or euros out of her account, you want
to deduct the money from her account and keep a permanent record of the new balance. Information
like this might be stored in something called a database .
However, there are other cases where you don't want to permanently store data, but simply want to
keep a temporary note of it. Let's look at an example. Say Ms. Bloggs has a loan from BigBadBank Inc.,
and she wants to fi nd out how much is still outstanding on this loan. She goes to the online banking
page for loans and clicks a link to fi nd out how much she owes. This is data that will be stored per-
manently somewhere. However, suppose you also provide a facility for increasing loan repayments to
pay off the loan early. If Ms. Bloggs enters an increased repayment amount into the text box on the web
page, you might want to show how much sooner the loan will be paid. This will involve a few possibly
complex calculations, so to make it easier, you want to write code that calculates the result in several
stages, storing the result at each stage as you go along, before providing a fi nal result. After you've done
the calculation and displayed the results, there's no need to permanently store the results for each stage,
so rather than use a database, you need to use something called a variable . Why is it called a variable?
Well, perhaps because a variable can be used to store temporary data that can be altered, or varied.
Another bonus of variables is that unlike permanent storage, which might be saved to disk or magnetic
tape, variables are held in the computer's memory. This means that it is much, much faster to store and
retrieve the data.
So what makes variables good places for temporarily storing your data? Well, variables have a limited
lifetime. When your visitors close the page or move to a new one, your variables are lost, unless you
take some steps to save them somewhere.
Each variable is given a name so that you can refer to it elsewhere in your code. These names must fol-
low certain rules.
Search WWH ::




Custom Search