Java Reference
In-Depth Information
visible only inside that function — no code outside the function can access them. So, for example, you
could declare a variable degCent in every function you have on a page and once on the page outside
any function. However, you can't declare the variable more than once inside any one function or more
than once on the page outside the functions. Note that reusing a variable name throughout a page in this
way, although not illegal, is not standard good practice — it can make the code very confusing to read.
Function parameters are similar to variables: They can't be seen outside the function, and although you
can declare a variable in a function with the same name as one of its parameters, it would cause a lot of
confusion and might easily lead to subtle bugs being overlooked. It's therefore bad coding practice and
best avoided, if only for the sake of your sanity when it comes to debugging!
So what happens when the code inside a function ends and execution returns to the point at which the
code was called? Do the variables defi ned within the function retain their value when you call the func-
tion the next time?
The answer is no: Variables not only have the scope property — where they are visible — but they also
have a lifetime . When the function fi nishes executing, the variables in that function die and their values are
lost, unless you return one of them to the calling code. Every so often JavaScript performs garbage collec-
tion (which we talked about in Chapter 2), whereby it scans through the code and sees if any variables
are no longer in use; if so, the data they hold are freed from memory to make way for the data of other
variables.
Given that global variables can be used anywhere, why not make all of them global? Global variables
are great when you need to keep track of data on a global basis. However, because they are available for
modifi cation anywhere in your code, it does mean that if they are changed incorrectly due to a bug, that
bug could be anywhere within the code, making debugging diffi cult. It's best, therefore, to keep global
variable use to a minimum, though sometimes they are a necessary evil — for example, when you need
to share data among different functions.
Summary
In this chapter you have concluded your look at the core of the JavaScript language and its syntax.
Everything from now on builds on these foundations, and with the less interesting syntax under
your belt, you can move on to more interesting things in the remainder of the topic.
The chapter looked at the following:
Decision making with the
if and switch statements. The ability to make decisions is essen-
tially what gives the code its “intelligence.” Based on whether a condition is true or false, you
can decide on a course of action to follow.
Comparison operators.
The comparison operators compare the value on the left of the opera-
tor (left-hand side, LHS) with the value on the right of the operator (right-hand side, RHS) and
return a Boolean value. Here is a list of the main comparison operators:
==
is the LHS equal to the RHS?
is the LHS not equal to the RHS?
!=
<=
is the LHS less than or equal to the RHS?
Search WWH ::




Custom Search