Escolar Documentos
Profissional Documentos
Cultura Documentos
Learning Goals
Understand the purpose of variables
Declare and assign value to variables
Understand the JavaScript variables types
Use variables in your scripts
What is a Variable?
A variable represents or holds a value
In other words, its a container
Actual can be changed at any time
What is a Variable?
Variables in JavaScript similar to those in
mathematics
Give variable a name, assign values
If value of variable changes, something
will happen within the script
Example of a Variable
x=2
Example of a Variable
x is name of variable
Assigned a value of 2
To change value of variable, give x new
assignment
Example of a Variable
x=4
Example of a Variable
Name of variable remains same, but
represents different value
Defining Variables
Declaring variables
Assigning values to variables
Naming variables
Declaring Variables
Create variables by declaring them
Use var keyword to declare text as
variable
Var keyword tells browser that text to
follow is name of new variable
Declaring Variables
var variablename;
Declaring Variables
var car;
var height;
var name, address, phoneNumber;
keywor
d
variable
assignmen
t operator
Naming Variables
Factors to consider when naming
variables:
Case sensitivity
Invalid characters
Reserved names
Easy to remember and meaningful names
4 different variables
delete
function
null
throw
boolean
do
goto
package
throws
break
double
if
private
transient
byte
else
implements protected
true
case
enum
import
public
try
catch
export
in
return
typeof
char
extends
instanceof
short
var
class
false
int
static
void
const
final
interface
super
volatile
continue
finally
long
switch
while
debugger
float
native
synchronized with
default
for
new
this
Variable Types
In JavaScript, variable values, or variable
types (a.k.a. data types) can include:
Numbers
Strings
Booleans
Nulls
Variable Types
Unlike stricter programming languages (C,
C++, Java), JavaScript does not force you
to declare the type of variable when you
define it
JavaScript allows any value to be
assigned to any variable
Number Variables
Any number, e.g., 7, -2, 3.453
Do not need to declare variables as
integers, floating-point numbers
Number Variables
var variablename = number;
var salary = 15000;
var phonebill = 5000;
var savings = 0;
var sparetime = -20;
Number Variables
For long numbers, use exponential
notation
452,000
Number Variables
4.52 x 105 (452,000)
String Variables
String variables represent a string of text
String can contain
Letters
Words
Spaces
Numbers
Symbols
String Variables
var variablename = stringtext;
String Variables
var myCar = Toyota
var myFutureCar = My dream car is a BMW
var myComputer = Pentium 4, 3GHz, 500MB
RAM, 80GB Hard Drive
var mySlang = @#*%$#!
Boolean Variables
Boolean variable has value of either true
or false, or 1 (true) or 0 (false)
var variablename = booleanvalue;
var bike = true;
var car = false;
Null Variables
Means that variable has no value
It is not a space, nor is it a zero
var variablename = null;
Adding Commands
document.write(headText);
document.write(beginEffect + myIntro + endEffect);
document.write(newSection);
document.write(linkTag);
document.write(newSection);
document.write(redText)
<HTML>
<HEAD>
<TITLE>A Page of JavaScript</TITLE>
</HEAD>
<BODY>
<SCRIPT language = JavaScript>
<!
var headingText = <H1>A Page of JavaScript</H1>;
document.write(headingText);
var myIntro = Hello, welcome to my JavaScript page!;
var linkTag = <A HREF=http://www.umt.edu.pk>UMT</A>;
var redText = <FONT color = red>I am so colorful today!</FONT>;
var beginEffect = <B>;
var endEffect = </B>;
var beginPara = <P>;
document.write(headText);
document.write(beginEffect + myIntro + endEffect);
document.write(newSection);
document.write(linkTag);
document.write(newSection);
document.write(redText)
//-->
</SCRIPT>
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>A Page of JavaScript</TITLE>
</HEAD>
<BODY>
<SCRIPT language = JavaScript>
<!
var headingText = <H1>A Page of JavaScript</H1>;
document.write(headingText);
var myIntro = Hello, welcome to my JavaScript page!;
var linkTag = <A HREF=http://www.umt.edu.pk>UMT</A>;
var redText = <FONT color = red>I am so colorful today!</FONT>;
var beginEffect = <I>;
var endEffect = </I>;
var beginPara = <BR>;
document.write(headText);
document.write(beginEffect + myIntro + endEffect);
document.write(newSection);
document.write(linkTag);
document.write(newSection);
document.write(redText)
//-->
</SCRIPT>
</BODY>
</HTML>