Java Reference
In-Depth Information
Then you assign string2 the value that's contained in string1 . To prove that the assignment has really
worked, you again use the alert() function to show the user the contents of each variable:
string2 = string1;
alert(string1);
alert(string2);
Next, you set string1 to a new value:
string1 = "Now for something different";
This leaves string2 with its current value, demonstrating that string2 has its own copy of the data
assigned to it from string1 in the previous step. You see in later chapters that this is not always the
case. However, as a general rule, basic data types, such as text and numbers, are always copied when
assigned, whereas more complex data types, like the objects you come across in Chapter 5, are actually
shared and not copied. For example, if you have a variable with the string Hello and assign five other
variables the value of this variable, you now have the original data and five independent copies of the
data. However, if it was an object rather than a string and you did the same thing, you'd find you still
have only one copy of the data, but that six variables share it. Changing the data using any of the six
variable names would change it for all the variables.
Finally, you use the alert() function to show the current values of each variable:
alert(string1);
alert(string2);
using data—CalCulations and BasiC string
manipulation
You've seen how to declare variables and how they can store information, but so far you haven't done
anything really useful with this knowledge—so just why would you want to use variables at all?
What variables enable you to do is temporarily hold information that you can use for processing in
mathematical calculations, in building up text messages, or in processing words that the user has
entered. Variables are a little bit like the Memory Store button on the average pocket calculator. Say
you were adding up your finances. You might first add up all the money you needed to spend, and
then store it in temporary memory. After you had added up all your money coming in, you could
deduct the amount stored in the memory to figure out how much would be left over. You can use
variables in a similar way: You can first gain the necessary user input and store it in variables, and
then you can do your calculations using the values obtained.
In this section you see how you can put the values stored in variables to good use in both number‐
crunching and text‐based operations.
numerical Calculations
JavaScript has a range of basic mathematical capabilities, such as addition, subtraction,
multiplication, and division. Each of the basic math functions is represented by a symbol: plus
 
Search WWH ::




Custom Search