Variables

advertisement
Variables:
In today’s class, I want to talk about Javascript variables. Why we need talk about variables?
Since variables are very important in javascript, when we program, we use variables everywhere.
Without variables, we can do nothing. Now , you may want to ask what are variables? Good
question. Variables are like storage units, which can be used to hold values or expressions.
X= 5; values
X=Y+Z; expressions
Then we talk about how to name variables. There are some proper syntax for us to follow. First,
variable names start with a letter or underscore(“_”), subsequent characters can be digits(0-9)
or letters(A-Z and/or a-z).
Eg: Number1, _name, time99
Remember, Javascript is case-senstive, so variable names are also case-sensitive. This means
that Myvariable and myvariable are two different names to javascript, because they have
different capitalization.
Of course, a variable can have a short name, like x, or a more descriptive name, like carname.
It is ideal to name a variable something that is logical, so that you will remember what you
are using it for. For example, if you were writing a program to divide 2 numbers, it could be
confusing if you called your variables x, y, z because you may forget which one is the divisor,
which one is the dividend and which one is quotient. A more logical approach would be name
them just that : divisor, dividend, quotient.
In javascript, we use keyword var to declare variables,
var x;
var divisor;
if you declare a variable within a function, it is called a local variable, because it is available
only within the function. But if you declare a variable outside the function, which means this
variable is available everywhere in current document, we call this type of variable global
variables. Therefore, the difference of global variables and local variables is the location of
declaration in the document.
After you declare your variable, you already have a variable which can be used store a value
or expression, but before you assign the value to your variable, your variable is empty. In
javascript, there are four types of values you can assign to your variables. First one is
numbers, such as 30, 1.15. Second one is Logical values, either true or false. Third one is
strings, like “hello!”. Last one is NULL, a special keyword which refers to nothing.
Eg
var dividend = 8;
var divisor = 4;
var mystring = “I may use this message multiple times.”;
var message = mystring;
Now, you already have your variables with assigned values, so you can use them. But before
you begin, I want to mention that it is important to think about the design of your program.
You need create appropriate variables so that it makes your life easier when you go to write
your program. For instance, if you know that you will be coding a lot of the same strings in a
message, you may want to create a variable called message and give it the value of your
message. That way, when you call it in your program, you do not have to retype the same
sentence over and over again, and if you want to change the content of the message, you only
have to change it once-in the variable declaration.
Javascript is a loosely typed language. That means you do not have to specify the data type of
a variable when you declare it, and data type are converted automatically as needed during
script execution.
For example,
You could declare a variable
Var answer = 4;
And later, you could assign the same variable a string value,
answer = “the answer is”
Since Javascript is loosely typed, this assignment does not cause any error message. However,
this is not good coding. You should create variables for a specific type, such as string, integer,
and be consistent in the values that you store in the variable.
In expressions involving numeric and string values, javascript converts the numeric values to
strings.
For example
X = “the answer is “ + 4;
+ tells javascript to concatenate, or stick together the two strings.
X becomes the string “the answer is 4”.
Related documents
Download