7.1 Java Script - Variables
OBJECTIVE
-
Define variables.
-
Define the types of variables.
-
Define the following terms: global, local, assign, declare,
initialize.
DISCUSSION
Variables
-
"Containers" for holding data.
-
Declare using keyword var
-
Can initialize when declare
-
Assign value in code
Variable Types
| Type |
Description |
Examples |
| Integer |
Holds whole numbers, positive or negative
|
iCount = 23
iTemp = -11
|
| Float |
Holds real numbers (numbers with decimal points, positive or negative) |
fTemp = 98.6
fAvogadro = 6.02E23
|
| String |
Holds characters |
sName = "Me"
sAddress = "100 Kenwood Ave"
|
| Boolean |
Only true or false. Can use 0 to represent false and 1 to represent true. |
bFlag = true |
Demo - Variable Types
Naming Conventions
-
Must start with a letter or underscore ("_").
-
Can not have punctuation characters.
-
Can not have spaces
-
Can not be the same as a "reserved" word
-
Should follow consistent naming convention. For example...
-
Start each variable with lowercase letter representing
variable type (i, s, f, b) or "g" and (i, s, f, or b) if the variable is
global.
-
Capitalize first letter of words
Constants
-
Assign a variable a value and do not change in program.
-
Make it easier to maintain program if defined once and used in multiple
locations.
-
Example:
Scope - Global vs Local Variables
-
Global variables are declared outside of functions.
-
Global variables can be accessed from any function.
-
Local variables can only be accessed locally in the function that it is
declared.
-
If you do not declare a variable then it is automatically global. This is not
good and is poor programming practice.
Demo - Global and Local Variables
EXERCISES
1. Which of the following names are NOT valid? num, num1,
1num, _num, n&um, iNum, num 1.
2. Write statements to declare and initialize variables
to...
-
Store the name of someone. Initialize it to your name.
-
Store the number of students in a class. Initialize it
to the number in this class.
-
Store your class average.
-
Store whether or not a number is maximum. Initialize it
to "false".
3. Explain the difference between a global and local
variable. Which is preferred? Where is each declared?
4. Give examples of when you would use a local variable
and a global variable.
5. The purpose of this exercise is to determine what
happens when you add variables of different types. For example, what will
be the result of adding a string to a boolean? You will answer this
question by creating a page that has 8 variables declared and initialized in a
<script> block in the <head>. There should be two variables
of each type. Then you should create a table as shown below.
In each of the empty cells, the variable from the column and row
should be added and written into the cell using document.write.
|
iX = 2 |
fX = 3.14 |
sX = "jo" |
bX = true |
| iY = 4 |
|
|
|
|
| fY = 2.2 |
|
|
|
|
| sY = "bob" |
|
|
|
|
| bY = false |
|
|
|
|
6. Create a program to demonstrate how global variables
can be accessed from all functions. Make sure that your page meets the
following requirements:
-
It has a textbox, a button labelled "Assign" and a button
labelled "Display".
-
It has a variable declared globally in the <script>
block.
-
It has a function called assign(). In this function, the
value from the textbox should be assigned to the global variable.
-
It has a function called display(). In this function,
the global variable is displayed in an alert.
-
When the user clicks the "Assign" button, the function
assign() is called.
-
When the user clicks on the "Display" button, the function
display() is called.
7. Create a program to demonstrate how local variables
can NOTbe accessed from all functions. Make sure that your page meets the
following requirements:
-
It has a textbox, a button labelled "Assign" and a button
labelled "Display".
-
It has a function called assign(). In this function a variable is
declared and assigned the value from the textbox.
-
It has a function called display(). In this function,
the variable in the function assign() is displayed in an alert.
-
When the user clicks the "Assign" button, the function
assign() is called.
-
When the user clicks on the "Display" button, the function
display() is called.
8. Determine the largest possible value for a
float. You can either search the Internet or create a program.
LINKS AND HELP